2000-01-18 01:55:52 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
|
|
|
* Copyright (c) 2000, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 01/17/2000 aliu Ported from Java.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
#include "unicode/hangjamo.h"
|
|
|
|
#include "unicode/rep.h"
|
|
|
|
#include "unicode/unifilt.h"
|
|
|
|
#include "unicode/unicode.h"
|
|
|
|
|
|
|
|
// These constants are from the Unicode book's algorithm.
|
|
|
|
|
|
|
|
#define SBase (0xAC00)
|
|
|
|
#define LBase (0x1100)
|
|
|
|
#define VBase (0x1161)
|
|
|
|
#define TBase (0x11A7)
|
|
|
|
#define LCount (19)
|
|
|
|
#define VCount (21)
|
|
|
|
#define TCount (28)
|
|
|
|
#define NCount (VCount * TCount) // 588
|
|
|
|
#define SCount (LCount * NCount) // 11172
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ID for this transliterator.
|
|
|
|
*/
|
|
|
|
const char* HangulJamoTransliterator::_ID = "Hangul-Jamo";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a transliterator.
|
|
|
|
*/
|
|
|
|
HangulJamoTransliterator::HangulJamoTransliterator(UnicodeFilter* adoptedFilter) :
|
|
|
|
Transliterator(_ID, adoptedFilter) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
HangulJamoTransliterator::HangulJamoTransliterator(const HangulJamoTransliterator& o) :
|
|
|
|
Transliterator(o) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
|
|
|
HangulJamoTransliterator& HangulJamoTransliterator::operator=(
|
|
|
|
const HangulJamoTransliterator& o) {
|
|
|
|
Transliterator::operator=(o);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterator API.
|
|
|
|
*/
|
|
|
|
Transliterator* HangulJamoTransliterator::clone(void) const {
|
|
|
|
return new HangulJamoTransliterator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2000-01-18 18:27:27 +00:00
|
|
|
* Implements {@link Transliterator#handleTransliterate}.
|
2000-01-18 01:55:52 +00:00
|
|
|
*/
|
2000-01-19 19:02:10 +00:00
|
|
|
void HangulJamoTransliterator::handleTransliterate(Replaceable& text, Position& offsets,
|
|
|
|
bool_t isIncremental) const {
|
|
|
|
int32_t cursor = offsets.cursor;
|
|
|
|
int32_t limit = offsets.limit;
|
2000-01-18 01:55:52 +00:00
|
|
|
|
|
|
|
UnicodeString replacement;
|
|
|
|
while (cursor < limit) {
|
|
|
|
UChar c = filteredCharAt(text, cursor);
|
|
|
|
if (decomposeHangul(c, replacement)) {
|
|
|
|
text.handleReplaceBetween(cursor, cursor+1, replacement);
|
|
|
|
cursor += replacement.length(); // skip over replacement
|
|
|
|
limit += replacement.length() - 1; // fix up limit
|
|
|
|
} else {
|
|
|
|
++cursor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-19 19:02:10 +00:00
|
|
|
offsets.limit = limit;
|
|
|
|
offsets.cursor = cursor;
|
2000-01-18 01:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool_t HangulJamoTransliterator::decomposeHangul(UChar s, UnicodeString& result) {
|
|
|
|
int32_t SIndex = s - SBase;
|
|
|
|
if (0 > SIndex || SIndex >= SCount) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
int32_t L = LBase + SIndex / NCount;
|
|
|
|
int32_t V = VBase + (SIndex % NCount) / TCount;
|
|
|
|
int32_t T = TBase + SIndex % TCount;
|
|
|
|
result.truncate(0);
|
|
|
|
result.append((UChar)L);
|
|
|
|
result.append((UChar)V);
|
|
|
|
if (T != TBase) {
|
|
|
|
result.append((UChar)T);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|