From 2fdd86040215cbe33587bdf39d3214e5aa20f00e Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 3 May 2014 13:13:23 -0700 Subject: [PATCH] Add encodeUnits() to utf8/utf16/sjis. So that we know how many units (e.g. u16s) are needed to encode. --- native.vcxproj | 4 +++- native.vcxproj.filters | 6 ++++++ util/text/shiftjis.h | 7 +++++++ util/text/utf16.h | 10 +++++++++- util/text/utf8.h | 12 ++++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/native.vcxproj b/native.vcxproj index cdc424ad84..2de8185ade 100644 --- a/native.vcxproj +++ b/native.vcxproj @@ -287,6 +287,8 @@ + + @@ -776,4 +778,4 @@ - + \ No newline at end of file diff --git a/native.vcxproj.filters b/native.vcxproj.filters index e9e15c5b0b..8406a16f12 100644 --- a/native.vcxproj.filters +++ b/native.vcxproj.filters @@ -338,6 +338,12 @@ math\fast + + util + + + util + diff --git a/util/text/shiftjis.h b/util/text/shiftjis.h index 72f9438c48..e92df04db6 100644 --- a/util/text/shiftjis.h +++ b/util/text/shiftjis.h @@ -127,6 +127,13 @@ struct ShiftJIS { return 2; } + static int encodeUnits(uint32_t j) { + if ((j & ~0xFF) == 0) { + return 1; + } + return 2; + } + private: const char *c_; int index_; diff --git a/util/text/utf16.h b/util/text/utf16.h index 002886259d..2475b7292a 100644 --- a/util/text/utf16.h +++ b/util/text/utf16.h @@ -42,7 +42,7 @@ public: return len; } - int byteIndex() const { + int shortIndex() const { return index_; } @@ -58,6 +58,14 @@ public: } } + static int encodeUnits(uint32_t u) { + if (u >= 0x10000) { + return 2; + } else { + return 1; + } + } + private: const uint16_t *c_; int index_; diff --git a/util/text/utf8.h b/util/text/utf8.h index 32312ee35a..8a11eb6a28 100644 --- a/util/text/utf8.h +++ b/util/text/utf8.h @@ -44,6 +44,18 @@ public: static int encode(char *dest, uint32_t ch) { return u8_wc_toutf8(dest, ch); } + static int encodeUnits(uint32_t ch) { + if (ch < 0x80) { + return 1; + } else if (ch < 0x800) { + return 2; + } else if (ch < 0x10000) { + return 3; + } else if (ch < 0x110000) { + return 4; + } + return 0; + } private: const char *c_;