편집 요약 없음 |
(초성 버그 수정) |
||
(같은 사용자의 중간 판 하나는 보이지 않습니다) | |||
41번째 줄: | 41번째 줄: | ||
-- @noSelfInFile * | -- @noSelfInFile * | ||
function | function charCodeAt(char) | ||
local a, b, c = string.byte(char, 1, 3) | local a, b, c = string.byte(char, 1, 3) | ||
return (a - 224) * 4096 + (b - 128) * 64 + c - 128 | return (a - 224) * 4096 + (b - 128) * 64 + c - 128 | ||
end | |||
function charCodeBetween(first, last) | |||
return function(char) | |||
local code = charCodeAt(char) | |||
return charCodeAt(first) <= code and code <= charCodeAt(last) | |||
end | |||
end | end | ||
function p.convert(text, choseong, jungseong, jongseong) | function p.convert(text, choseong, jungseong, jongseong) | ||
50번째 줄: | 56번째 줄: | ||
"[^%c%p%s%w][^%c%p%s%w][^%c%p%s%w]", | "[^%c%p%s%w][^%c%p%s%w][^%c%p%s%w]", | ||
function(char) | function(char) | ||
local code = | local code = charCodeAt(char) | ||
local cho | if charCodeBetween("가", "힣")(char) then | ||
code = code - charCodeAt("가") | |||
local cho | |||
local jung | |||
local jong | |||
cho = math.floor(code / 588) | |||
code = code - cho * 588 | |||
jung = math.floor(code / 28) | |||
code = code - jung * 28 | |||
jong = code | |||
return (choseong[cho + 1] .. jungseong[jung + 1]) .. jongseong[jong + 1] | |||
elseif charCodeBetween("ㄱ", "ㅣ")(char) then | |||
code = code - charCodeAt("ㄱ") + 1 -- https://en.wikipedia.org/wiki/Hangul_Compatibility_Jamo | |||
local g,gg,n,d,dd,r,m,b,bb,s,ss,ng,j,jj,c,k,t,p,h = unpack(choseong) | |||
local g2,gg2,gs,n2,nj,nh,d2,r2,rg,rm,rb,rs,rt,rp,rh,m2,b2,bs,s2,ss2,ng2,j2,c2,k2,t2,p2,h2 = unpack(jongseong) | |||
local jamo = {g,gg,gs,n,nj,nh,d,dd,r,rg,rm,rb,rs,rt,rp,rh,m,b,bb,bs,s,ss,ng,j,jj,c,k,t,p,h,unpack(jungseong)} | |||
return jamo[code] | |||
end | |||
end | end | ||
) | ) |
2022년 6월 24일 (금) 21:50 기준 최신판
이 모듈에 대한 설명문서는 모듈:한글 처리/설명문서에서 만들 수 있습니다
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
---
local __TS__StringSplit
do
local sub = string.sub
local find = string.find
function __TS__StringSplit(source, separator, limit)
if limit == nil then
limit = 4294967295
end
if limit == 0 then
return {}
end
local result = {}
local resultIndex = 1
if separator == nil or separator == "" then
for i = 1, #source do
result[resultIndex] = sub(source, i, i)
resultIndex = resultIndex + 1
end
else
local currentPos = 1
while resultIndex <= limit do
local startPos, endPos = find(source, separator, currentPos, true)
if not startPos then
break
end
result[resultIndex] = sub(source, currentPos, startPos - 1)
resultIndex = resultIndex + 1
currentPos = endPos + 1
end
if resultIndex <= limit then
result[resultIndex] = sub(source, currentPos)
end
end
return result
end
end
p = { split = __TS__StringSplit }
-- @noSelfInFile *
function charCodeAt(char)
local a, b, c = string.byte(char, 1, 3)
return (a - 224) * 4096 + (b - 128) * 64 + c - 128
end
function charCodeBetween(first, last)
return function(char)
local code = charCodeAt(char)
return charCodeAt(first) <= code and code <= charCodeAt(last)
end
end
function p.convert(text, choseong, jungseong, jongseong)
local result = string.gsub(
text,
"[^%c%p%s%w][^%c%p%s%w][^%c%p%s%w]",
function(char)
local code = charCodeAt(char)
if charCodeBetween("가", "힣")(char) then
code = code - charCodeAt("가")
local cho
local jung
local jong
cho = math.floor(code / 588)
code = code - cho * 588
jung = math.floor(code / 28)
code = code - jung * 28
jong = code
return (choseong[cho + 1] .. jungseong[jung + 1]) .. jongseong[jong + 1]
elseif charCodeBetween("ㄱ", "ㅣ")(char) then
code = code - charCodeAt("ㄱ") + 1 -- https://en.wikipedia.org/wiki/Hangul_Compatibility_Jamo
local g,gg,n,d,dd,r,m,b,bb,s,ss,ng,j,jj,c,k,t,p,h = unpack(choseong)
local g2,gg2,gs,n2,nj,nh,d2,r2,rg,rm,rb,rs,rt,rp,rh,m2,b2,bs,s2,ss2,ng2,j2,c2,k2,t2,p2,h2 = unpack(jongseong)
local jamo = {g,gg,gs,n,nj,nh,d,dd,r,rg,rm,rb,rs,rt,rp,rh,m,b,bb,bs,s,ss,ng,j,jj,c,k,t,p,h,unpack(jungseong)}
return jamo[code]
end
end
)
return result
end
return p