choseong.tpz
// Choseong (Hangul lead-consonant) extractor. Pulls the leading consonant from each Hangul syllable. Written in Topaz.
// Key identifiers: 글자정리=escapeChar, 글=char, 줄정리=escapeLine, 줄=line, 출력=out, 초성변환=toChoseong,
// 텍스트=text, 초성표=choseongTable, 글자들=chars, 결과=result, 색인=index, 코드=code, 보기=view, 변환=converted
//
// 초성 추출기. 각 한글 음절에서 첫 자음(초성)을 뽑아냅니다. Topaz로 작성했습니다.
// 한글 음절 U+AC00..U+D7A3 은 초성 = (코드포인트 - 0xAC00) / 588 로 표현됩니다. 한글이 아닌 스칼라
// Hangul syllables U+AC00..U+D7A3 give choseong = (codepoint - 0xAC00) / 588. Non-Hangul scalars
// (공백, 문장부호, 영어, 이모지, 낱자 자모)는 그대로 통과합니다. 각 스칼라의 코드포인트가
// (whitespace, punctuation, English, emoji, lone jamo) pass through unchanged. Each scalar's codepoint
// 필요합니다. 이 앱이 Topaz에 도입시킨 기본 연산 `str.codePointAt(i)` 를 씁니다.
// is needed. This app uses the builtin `str.codePointAt(i)` it introduced to Topaz.
// 순수하며 결정적입니다. 같은 텍스트는 인터프리터와 네이티브/wasm 빌드에서 같은 HTML을 냅니다.
// Pure and deterministic. The same text yields the same HTML across interpreter and native/wasm builds.
function 글자정리(글: string) -> string {
if 글 == "&" { "&" }
else if 글 == "<" { "<" }
else if 글 == ">" { ">" }
else { 글 }
}
function 줄정리(줄: string) -> string {
let mut 출력 = ""
for 글 in 줄.scalars() {
출력 = 출력 + 글자정리(글)
}
출력
}
function 초성변환(텍스트: string) -> string {
let 초성표 = [
"ㄱ", "ㄲ", "ㄴ", "ㄷ", "ㄸ", "ㄹ", "ㅁ", "ㅂ", "ㅃ", "ㅅ",
"ㅆ", "ㅇ", "ㅈ", "ㅉ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ",
]
let mut 결과 = ""
for 글 in 텍스트.scalars() {
let 코드 = 글.codePointAt(0) ?? -1
if 코드 >= 44032 && 코드 <= 55203 {
// U+AC00..U+D7A3, 조합된 한글 음절입니다.
// U+AC00..U+D7A3, a precomposed Hangul syllable.
결과 = 결과 + 초성표[(코드 - 44032) / 588]
} else {
결과 = 결과 + 글
}
}
결과
}
function 보기(텍스트: string) -> string {
let 변환 = 초성변환(텍스트)
// 입력 반향과 초성 줄 둘 다 사용자에게서 나온 값입니다. 주입 전에 둘 다 HTML 이스케이프합니다.
// Both the echoed input and the choseong line come from the user. HTML-escape both before injecting.
let mut 결과 = "<div class=\"choseong\">"
결과 = 결과 + "<div class=\"src\">" + 줄정리(텍스트) + "</div>"
결과 = 결과 + "<div class=\"out\">" + 줄정리(변환) + "</div>"
결과 + "</div>"
}
print(보기(input()))