Index: base/utf_offset_string_conversions.cc |
=================================================================== |
--- base/utf_offset_string_conversions.cc (revision 82762) |
+++ base/utf_offset_string_conversions.cc (working copy) |
@@ -14,21 +14,18 @@ |
using base::ReadUnicodeCharacter; |
using base::WriteUnicodeCharacter; |
-// Generalized Unicode converter ----------------------------------------------- |
- |
// Converts the given source Unicode character type to the given destination |
// Unicode character type as a STL string. The given input buffer and size |
// determine the source, and the given output STL string will be replaced by |
// the result. |
-template<typename SRC_CHAR> |
-bool ConvertUnicode(const SRC_CHAR* src, |
+bool ConvertUnicode(const char* src, |
size_t src_len, |
- std::wstring* output, |
+ string16* output, |
std::vector<size_t>* offsets_for_adjustment) { |
if (offsets_for_adjustment) { |
std::for_each(offsets_for_adjustment->begin(), |
offsets_for_adjustment->end(), |
- LimitOffset<std::wstring>(src_len)); |
+ LimitOffset<string16>(src_len)); |
} |
// ICU requires 32-bit numbers. |
@@ -65,152 +62,50 @@ |
return success; |
} |
-// UTF-8 <-> Wide -------------------------------------------------------------- |
- |
-bool UTF8ToWideAndAdjustOffset(const char* src, |
- size_t src_len, |
- std::wstring* output, |
- size_t* offset_for_adjustment) { |
- std::vector<size_t> offsets; |
- if (offset_for_adjustment) |
- offsets.push_back(*offset_for_adjustment); |
- PrepareForUTF16Or32Output(src, src_len, output); |
- bool ret = ConvertUnicode(src, src_len, output, &offsets); |
- if (offset_for_adjustment) |
- *offset_for_adjustment = offsets[0]; |
- return ret; |
-} |
- |
-bool UTF8ToWideAndAdjustOffsets(const char* src, |
+bool UTF8ToUTF16AndAdjustOffset(const char* src, |
size_t src_len, |
- std::wstring* output, |
- std::vector<size_t>* offsets_for_adjustment) { |
- PrepareForUTF16Or32Output(src, src_len, output); |
- return ConvertUnicode(src, src_len, output, offsets_for_adjustment); |
-} |
- |
-std::wstring UTF8ToWideAndAdjustOffset(const base::StringPiece& utf8, |
- size_t* offset_for_adjustment) { |
- std::vector<size_t> offsets; |
- if (offset_for_adjustment) |
- offsets.push_back(*offset_for_adjustment); |
- std::wstring result; |
- UTF8ToWideAndAdjustOffsets(utf8.data(), utf8.length(), &result, |
- &offsets); |
- if (offset_for_adjustment) |
- *offset_for_adjustment = offsets[0]; |
- return result; |
-} |
- |
-std::wstring UTF8ToWideAndAdjustOffsets(const base::StringPiece& utf8, |
- std::vector<size_t>* |
- offsets_for_adjustment) { |
- std::wstring result; |
- UTF8ToWideAndAdjustOffsets(utf8.data(), utf8.length(), &result, |
- offsets_for_adjustment); |
- return result; |
-} |
- |
-// UTF-16 <-> Wide ------------------------------------------------------------- |
- |
-#if defined(WCHAR_T_IS_UTF16) |
- |
-// When wide == UTF-16, then conversions are a NOP. |
-bool UTF16ToWideAndAdjustOffset(const char16* src, |
- size_t src_len, |
- std::wstring* output, |
+ string16* output, |
size_t* offset_for_adjustment) { |
- output->assign(src, src_len); |
- if (offset_for_adjustment && (*offset_for_adjustment >= src_len)) |
- *offset_for_adjustment = std::wstring::npos; |
- return true; |
-} |
- |
-bool UTF16ToWideAndAdjustOffsets(const char16* src, |
- size_t src_len, |
- std::wstring* output, |
- std::vector<size_t>* offsets_for_adjustment) { |
- output->assign(src, src_len); |
- if (offsets_for_adjustment) { |
- std::for_each(offsets_for_adjustment->begin(), |
- offsets_for_adjustment->end(), |
- LimitOffset<std::wstring>(src_len)); |
- } |
- return true; |
-} |
- |
-std::wstring UTF16ToWideAndAdjustOffset(const string16& utf16, |
- size_t* offset_for_adjustment) { |
- if (offset_for_adjustment && (*offset_for_adjustment >= utf16.length())) |
- *offset_for_adjustment = std::wstring::npos; |
- return utf16; |
-} |
- |
-std::wstring UTF16ToWideAndAdjustOffsets( |
- const string16& utf16, |
- std::vector<size_t>* offsets_for_adjustment) { |
- if (offsets_for_adjustment) { |
- std::for_each(offsets_for_adjustment->begin(), |
- offsets_for_adjustment->end(), |
- LimitOffset<std::wstring>(utf16.length())); |
- } |
- return utf16; |
-} |
- |
-#elif defined(WCHAR_T_IS_UTF32) |
- |
-bool UTF16ToWideAndAdjustOffset(const char16* src, |
- size_t src_len, |
- std::wstring* output, |
- size_t* offset_for_adjustment) { |
std::vector<size_t> offsets; |
if (offset_for_adjustment) |
offsets.push_back(*offset_for_adjustment); |
- output->clear(); |
- // Assume that normally we won't have any non-BMP characters so the counts |
- // will be the same. |
- output->reserve(src_len); |
+ PrepareForUTF16Or32Output(src, src_len, output); |
bool ret = ConvertUnicode(src, src_len, output, &offsets); |
if (offset_for_adjustment) |
*offset_for_adjustment = offsets[0]; |
return ret; |
} |
-bool UTF16ToWideAndAdjustOffsets(const char16* src, |
+bool UTF8ToUTF16AndAdjustOffsets(const char* src, |
size_t src_len, |
- std::wstring* output, |
+ string16* output, |
std::vector<size_t>* offsets_for_adjustment) { |
- output->clear(); |
- // Assume that normally we won't have any non-BMP characters so the counts |
- // will be the same. |
- output->reserve(src_len); |
+ PrepareForUTF16Or32Output(src, src_len, output); |
return ConvertUnicode(src, src_len, output, offsets_for_adjustment); |
} |
-std::wstring UTF16ToWideAndAdjustOffset(const string16& utf16, |
+string16 UTF8ToUTF16AndAdjustOffset(const base::StringPiece& utf8, |
size_t* offset_for_adjustment) { |
std::vector<size_t> offsets; |
if (offset_for_adjustment) |
offsets.push_back(*offset_for_adjustment); |
- std::wstring result; |
- UTF16ToWideAndAdjustOffsets(utf16.data(), utf16.length(), &result, |
+ string16 result; |
+ UTF8ToUTF16AndAdjustOffsets(utf8.data(), utf8.length(), &result, |
&offsets); |
if (offset_for_adjustment) |
*offset_for_adjustment = offsets[0]; |
return result; |
} |
-std::wstring UTF16ToWideAndAdjustOffsets( |
- const string16& utf16, |
+string16 UTF8ToUTF16AndAdjustOffsets( |
+ const base::StringPiece& utf8, |
std::vector<size_t>* offsets_for_adjustment) { |
- std::wstring result; |
- UTF16ToWideAndAdjustOffsets(utf16.data(), utf16.length(), &result, |
+ string16 result; |
+ UTF8ToUTF16AndAdjustOffsets(utf8.data(), utf8.length(), &result, |
offsets_for_adjustment); |
return result; |
} |
-#endif // defined(WCHAR_T_IS_UTF32) |
- |
AdjustOffset::Adjustment::Adjustment(size_t location, |
size_t old_length, |
size_t new_length) |
@@ -222,20 +117,20 @@ |
: adjustments_(adjustments) {} |
void AdjustOffset::operator()(size_t& offset) { |
- if (offset == std::wstring::npos) |
+ if (offset == string16::npos) |
return; |
size_t adjustment = 0; |
for (Adjustments::const_iterator i = adjustments_.begin(); |
i != adjustments_.end(); ++i) { |
size_t location = i->location; |
if (offset == location && i->new_length == 0) { |
- offset = std::wstring::npos; |
+ offset = string16::npos; |
return; |
} |
if (offset <= location) |
break; |
if (offset < (location + i->old_length)) { |
- offset = std::wstring::npos; |
+ offset = string16::npos; |
return; |
} |
adjustment += (i->old_length - i->new_length); |