#!/usr/bin/env python3
"""Measurements for the YouTube title and description length guide. Prints the log lines the page quotes.
Controls first: strings whose counts are known in advance. If a control fails the script stops (exit 3)."""
import sys, unicodedata
def c(s): return len(s), len(s.encode('utf-16-le'))//2, len(s.encode('utf-8'))
controls = [('a'*100,(100,100,100)), ('é',(1,1,2)), ('\U0001F600',(1,2,4)), ('क',(1,1,3)), ('動',(1,1,3)), ('д',(1,1,2))]
for s,exp in controls:
    got=c(s)
    print('control', repr(s)[:20], 'expected', exp, 'got', got, 'PASS' if got==exp else 'FAIL')
    if got!=exp: sys.exit(3)
print()
samples = [
 ('plain English', 'How to edit video on a phone'),
 ('accent, one code point per letter (NFC)', unicodedata.normalize('NFC','Café crème brûlée')),
 ('same words, accents as separate marks (NFD)', unicodedata.normalize('NFD','Café crème brûlée')),
 ('three fire emoji', 'I tried it \U0001F525\U0001F525\U0001F525'),
 ('family emoji joined with ZWJ', 'Family vlog \U0001F468‍\U0001F469‍\U0001F467'),
 ('flag emoji (two regional indicators)', 'Tokyo trip \U0001F1EF\U0001F1F5'),
 ('Hindi (Devanagari)', 'वीडियो एडिटिंग सीखें'),
 ('Japanese', '動画編集のやり方'),
 ('Russian (Cyrillic)', 'Как снять видео'),
]
print('TABLE 1: one title per row, three counts')
for name,s in samples:
    cp,u16,b=c(s); print(f'{name} | {cp} code points | {u16} UTF-16 units | {b} UTF-8 bytes')
print()
print('TABLE 2: how many of one symbol fit in 5000 bytes (Data API description) and in 100 (title, by UTF-16 units)')
for name,ch in [('ASCII letter a','a'),('accented letter é','é'),('Cyrillic д','д'),('Devanagari क','क'),('Japanese 動','動'),('emoji \U0001F600','\U0001F600')]:
    cp,u16,b=c(ch)
    n_bytes=5000//b; n_title=100//u16
    full=ch*n_bytes; ok=len(full.encode('utf-8'))<=5000 and len((full+ch).encode('utf-8'))>5000
    print(f'{name} | {b} bytes each | {n_bytes} fit in 5000 bytes | {u16} UTF-16 units each | {n_title} fit in a 100-unit title | boundary check {"PASS" if ok else "FAIL"}')
print()
print('LINE BREAKS: a two-line text')
for name,s in [('LF','line one\nline two'),('CRLF','line one\r\nline two')]:
    print(name, c(s))
print()
print('NORMALISATION: NFC vs NFD of the same accented title')
a,b2=samples[1][1],samples[2][1]
print('visually same:', unicodedata.normalize('NFC',b2)==a, '| code points', len(a), 'vs', len(b2))
