From 34dab07ed6e826666fd0589069f2c9b5bd2ba4dc Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Wed, 8 Aug 2018 17:49:02 +0000 Subject: Move ByteString::FromUnicode() to WideString::ToDefANSI() Turns out that "FromUnicode" is misleading in that, on linux, it simply removes any characters beyond 0xFF and passes the rest unchanged, so no unicode decoding actually takes place. On Windows, it passes it into the system function specifying FX_CODEPAGE_DefANSI, converting it into the so-called "default ANSI code plane", passing some characters, converting others to '?' and still others to 'A'. Either way, nothing resembling UTF8 comes out of this, so pick a better name. These now immediately look suspicious, so a follow-up CL will see which ones should really be WideString::UTF8Encode() instead. Making this a normal method on a widestring rather than a static method on a bytestring feels more natural; this is parallel to the UTF8Encode and UTF16LE_Encode functions. Add a test that shows these conversions. Change-Id: Ia7551b47199eba61b5c328a97bfe9176ac8e583c Reviewed-on: https://pdfium-review.googlesource.com/39690 Reviewed-by: Lei Zhang Commit-Queue: Tom Sepez --- fxjs/cjs_global.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'fxjs/cjs_global.cpp') diff --git a/fxjs/cjs_global.cpp b/fxjs/cjs_global.cpp index 567d54853c..1865f57ddb 100644 --- a/fxjs/cjs_global.cpp +++ b/fxjs/cjs_global.cpp @@ -232,7 +232,7 @@ CJS_Return CJS_Global::QueryProperty(const wchar_t* propname) { CJS_Return CJS_Global::DelProperty(CJS_Runtime* pRuntime, const wchar_t* propname) { - auto it = m_MapGlobal.find(ByteString::FromUnicode(propname)); + auto it = m_MapGlobal.find(WideString(propname).ToDefANSI()); if (it == m_MapGlobal.end()) return CJS_Return(JSMessage::kUnknownProperty); @@ -242,7 +242,7 @@ CJS_Return CJS_Global::DelProperty(CJS_Runtime* pRuntime, CJS_Return CJS_Global::GetProperty(CJS_Runtime* pRuntime, const wchar_t* propname) { - auto it = m_MapGlobal.find(ByteString::FromUnicode(propname)); + auto it = m_MapGlobal.find(WideString(propname).ToDefANSI()); if (it == m_MapGlobal.end()) return CJS_Return(); @@ -272,7 +272,7 @@ CJS_Return CJS_Global::GetProperty(CJS_Runtime* pRuntime, CJS_Return CJS_Global::SetProperty(CJS_Runtime* pRuntime, const wchar_t* propname, v8::Local vp) { - ByteString sPropName = ByteString::FromUnicode(propname); + ByteString sPropName = WideString(propname).ToDefANSI(); if (vp->IsNumber()) { return SetGlobalVariables(sPropName, JS_GlobalDataType::NUMBER, pRuntime->ToDouble(vp), false, "", @@ -284,10 +284,9 @@ CJS_Return CJS_Global::SetProperty(CJS_Runtime* pRuntime, v8::Local(), false); } if (vp->IsString()) { - return SetGlobalVariables( - sPropName, JS_GlobalDataType::STRING, 0, false, - ByteString::FromUnicode(pRuntime->ToWideString(vp)), - v8::Local(), false); + return SetGlobalVariables(sPropName, JS_GlobalDataType::STRING, 0, false, + pRuntime->ToWideString(vp).ToDefANSI(), + v8::Local(), false); } if (vp->IsObject()) { return SetGlobalVariables(sPropName, JS_GlobalDataType::OBJECT, 0, false, @@ -310,8 +309,7 @@ CJS_Return CJS_Global::setPersistent( if (params.size() != 2) return CJS_Return(JSMessage::kParamError); - auto it = m_MapGlobal.find( - ByteString::FromUnicode(pRuntime->ToWideString(params[0]))); + auto it = m_MapGlobal.find(pRuntime->ToWideString(params[0]).ToDefANSI()); if (it == m_MapGlobal.end() || it->second->bDeleted) return CJS_Return(JSMessage::kGlobalNotFoundError); @@ -432,7 +430,7 @@ void CJS_Global::ObjectToArray(CJS_Runtime* pRuntime, continue; } if (v->IsString()) { - ByteString sValue = ByteString::FromUnicode(pRuntime->ToWideString(v)); + ByteString sValue = pRuntime->ToWideString(v).ToDefANSI(); CJS_KeyValue* pObjElement = new CJS_KeyValue; pObjElement->nType = JS_GlobalDataType::STRING; pObjElement->sKey = sKey; -- cgit v1.2.3