Skip to content

Instantly share code, notes, and snippets.

@ohgyun
Created April 3, 2014 06:54
Show Gist options
  • Save ohgyun/9949475 to your computer and use it in GitHub Desktop.
Save ohgyun/9949475 to your computer and use it in GitHub Desktop.
초성 비교 (초성과 완성된 문자열을 비교한다)
function isChosungMatch(query, target) {
var lq = query.length; // length of query
var lt = target.length; // length of target
var ldiff = lt - lq; // length diff
var it = 0; // index of target
var iq = 0; // index of query
var cq; // character of query
var ct; // character of target
// 쿼리가 없으면 모두 매칭했다고 본다
if (lq === 0) {
return true;
}
// 쿼리가 대상 문자보다 길면 매칭할 수 없다
if (ldiff < 0) {
return false;
}
// 타겟 문자열을 돌며 매칭 여부를 확인한다
for (; it <= ldiff; it++) {
iq = 0;
while (iq < lq) {
cq = query[iq];
ct = target[it + iq];
// 쿼리가 초성이고 타겟이 한글이면
if (_isChosung(cq) && _isHangul(ct)) {
// 초성이 매치되는지 확인한다
if (cq === _getChosung(ct)) {
iq++;
} else {
break;
}
// 초성이 아니라면 직접 비교한다
} else {
if (cq === ct) {
iq++;
} else {
break;
}
}
}
// 모두 매칭한 경우
if (iq === lq) {
return true;
}
}
// 찾지 못한 경우
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment