Skip to content

Commit

Permalink
Deprecate EncodingUtil::UTF8ToSJIS.
Browse files Browse the repository at this point in the history
This is a series of CLs to drop dependency on iconv (#252).

UTF8ToSJIS is used only in Windows build.  We can move it from
src/base/encoding_util.cc to src/win32/base/string_util.cc to in
favor of simplicity.

This is just a refactoring.  No behavior change is intended.

BUG=#252
TEST=unittest
REF_BUG=19010851
REF_CL=87124492
  • Loading branch information
yukawa committed Nov 9, 2015
1 parent 6b287d0 commit c29e55d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
4 changes: 0 additions & 4 deletions src/base/encoding_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ inline bool Convert(const char *from, const char *to,

namespace mozc {

void EncodingUtil::UTF8ToSJIS(const string &input, string *output) {
Convert("UTF8", "SJIS", input, output);
}

void EncodingUtil::SJISToUTF8(const string &input, string *output) {
Convert("SJIS", "UTF8", input, output);
}
Expand Down
1 change: 0 additions & 1 deletion src/base/encoding_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ namespace mozc {

class EncodingUtil {
public:
static void UTF8ToSJIS(const string &input, string *output);
static void SJISToUTF8(const string &input, string *output);

private:
Expand Down
7 changes: 0 additions & 7 deletions src/base/encoding_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ namespace {
#else
TEST(EncodingUtilTest, Issue2190350) {
string result = "";
// "\xE3\x81\x82" == Hiragana a in UTF8
EncodingUtil::UTF8ToSJIS("\xE3\x81\x82", &result);
EXPECT_EQ(2, result.length());
// "\x82\xA0" == Hiragana a in Shift-JIS
EXPECT_EQ("\x82\xA0", result);

result = "";
EncodingUtil::SJISToUTF8("\x82\xA0", &result);
EXPECT_EQ(3, result.length());
EXPECT_EQ("\xE3\x81\x82", result);
Expand Down
2 changes: 1 addition & 1 deletion src/mozc_version_template.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MAJOR=2
MINOR=17
BUILD=2224
BUILD=2225
REVISION=102
# NACL_DICTIONARY_VERSION is the target version of the system dictionary to be
# downloaded by NaCl Mozc.
Expand Down
33 changes: 31 additions & 2 deletions src/win32/base/string_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

#include <memory>

#include "base/encoding_util.h"
#include "base/util.h"
#include "protocol/commands.pb.h"

Expand All @@ -43,6 +42,36 @@ namespace {

const size_t kMaxReadingChars = 512;

void UTF8ToSJIS(StringPiece input, string *output) {
wstring utf16;
Util::UTF8ToWide(input, &utf16);
if (utf16.empty()) {
output->clear();
return;
}

const int kCodePageShiftJIS = 932;

const int output_length_without_null = ::WideCharToMultiByte(
kCodePageShiftJIS, 0, utf16.data(), utf16.size(), nullptr, 0, nullptr,
nullptr);
if (output_length_without_null == 0) {
output->clear();
return;
}

unique_ptr<char[]> sjis(new char[output_length_without_null]);
const int actual_output_length_without_null = ::WideCharToMultiByte(
kCodePageShiftJIS, 0, utf16.data(), utf16.size(), sjis.get(),
output_length_without_null, nullptr, nullptr);
if (output_length_without_null != actual_output_length_without_null) {
output->clear();
return;
}

output->assign(sjis.get(), actual_output_length_without_null);
}

} // namespace

wstring StringUtil::KeyToReading(StringPiece key) {
Expand All @@ -52,7 +81,7 @@ wstring StringUtil::KeyToReading(StringPiece key) {
DWORD lcid = MAKELCID(MAKELANGID(LANG_JAPANESE, SUBLANG_DEFAULT),
SORT_JAPANESE_XJIS);
string sjis;
mozc::EncodingUtil::UTF8ToSJIS(katakana, &sjis);
UTF8ToSJIS(katakana, &sjis);

// Convert "\x81\x65" (backquote in SJIFT-JIS) to ` by myself since
// LCMapStringA converts it to ' for some reason.
Expand Down

0 comments on commit c29e55d

Please sign in to comment.