Add Widestring::{To,From,Is}ASCII()
Straight widening/narrowing of 7-bit values with no funny business.
Precursor to converting some L"" literals containing only single-byte
values.
Change-Id: I811a87c92d806744dc4bfbe23eb6aa3e27057103
Reviewed-on: https://pdfium-review.googlesource.com/c/45792
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp
index 234270e..d699b42 100644
--- a/core/fxcrt/widestring.cpp
+++ b/core/fxcrt/widestring.cpp
@@ -635,7 +635,22 @@
return m_pData ? m_pData->m_nRefs : 0;
}
-// static
+bool WideString::IsASCII() const {
+ for (wchar_t wc : *this) {
+ if (wc <= 0 || wc > 127) // Questionable signedness of wchar_t.
+ return false;
+ }
+ return true;
+}
+
+ByteString WideString::ToASCII() const {
+ ByteString result;
+ result.Reserve(GetLength());
+ for (wchar_t wc : *this)
+ result.InsertAtBack(static_cast<char>(wc & 0x7f));
+ return result;
+}
+
ByteString WideString::ToDefANSI() const {
int src_len = GetLength();
int dest_len = FXSYS_WideCharToMultiByte(
@@ -864,6 +879,15 @@
}
// static
+WideString WideString::FromASCII(const ByteStringView& bstr) {
+ WideString result;
+ result.Reserve(bstr.GetLength());
+ for (char c : bstr)
+ result.InsertAtBack(static_cast<wchar_t>(c & 0x7f));
+ return result;
+}
+
+// static
WideString WideString::FromDefANSI(const ByteStringView& bstr) {
int src_len = bstr.GetLength();
int dest_len = FXSYS_MultiByteToWideChar(