Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Unified Diff: base/utf_offset_string_conversions.cc

Issue 6898026: Eliminate wstring from base/utf_offset_string_conversions.h, net/base/escape.h, and net/base/net_... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/utf_offset_string_conversions.cc
===================================================================
--- base/utf_offset_string_conversions.cc (revision 82762)
+++ base/utf_offset_string_conversions.cc (working copy)
@@ -14,26 +14,23 @@
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.
bool success = true;
- AdjustOffset::Adjustments adjustments;
+ OffsetAdjuster offset_adjuster(offsets_for_adjustment);
int32 src_len32 = static_cast<int32>(src_len);
for (int32 i = 0; i < src_len32; i++) {
uint32 code_point;
@@ -50,195 +47,98 @@
// character read, not after it (so that incrementing it in the loop
// increment will place it at the right location), so we need to account
// for that in determining the amount that was read.
- adjustments.push_back(AdjustOffset::Adjustment(
- original_i, i - original_i + 1, chars_written));
+ offset_adjuster.Add(OffsetAdjuster::Adjustment(original_i,
+ i - original_i + 1, chars_written));
}
}
-
- // Make offset adjustment.
- if (offsets_for_adjustment && !adjustments.empty()) {
- std::for_each(offsets_for_adjustment->begin(),
- offsets_for_adjustment->end(),
- AdjustOffset(adjustments));
- }
-
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)
+OffsetAdjuster::Adjustment::Adjustment(size_t original_offset,
+ size_t original_length,
+ size_t output_length)
+ : original_offset(original_offset),
+ original_length(original_length),
+ output_length(output_length) {
+}
-AdjustOffset::Adjustment::Adjustment(size_t location,
- size_t old_length,
- size_t new_length)
- : location(location),
- old_length(old_length),
- new_length(new_length) {}
+OffsetAdjuster::OffsetAdjuster(std::vector<size_t>* offsets_for_adjustment)
+ : offsets_for_adjustment_(offsets_for_adjustment) {
+}
-AdjustOffset::AdjustOffset(const Adjustments& adjustments)
- : adjustments_(adjustments) {}
+OffsetAdjuster::~OffsetAdjuster() {
+ if (!offsets_for_adjustment_ || adjustments_.empty())
+ return;
+ for (std::vector<size_t>::iterator i(offsets_for_adjustment_->begin());
+ i != offsets_for_adjustment_->end(); ++i)
+ AdjustOffset(i);
+}
-void AdjustOffset::operator()(size_t& offset) {
- if (offset == std::wstring::npos)
+void OffsetAdjuster::Add(const Adjustment& adjustment) {
+ adjustments_.push_back(adjustment);
+}
+
+void OffsetAdjuster::AdjustOffset(std::vector<size_t>::iterator offset) {
+ if (*offset == string16::npos)
return;
size_t adjustment = 0;
- for (Adjustments::const_iterator i = adjustments_.begin();
+ for (std::vector<Adjustment>::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;
+ if (*offset == i->original_offset && i->output_length == 0) {
+ *offset = string16::npos;
return;
}
- if (offset <= location)
+ if (*offset <= i->original_offset)
break;
- if (offset < (location + i->old_length)) {
- offset = std::wstring::npos;
+ if (*offset < (i->original_offset + i->original_length)) {
+ *offset = string16::npos;
return;
}
- adjustment += (i->old_length - i->new_length);
+ adjustment += (i->original_length - i->output_length);
}
- offset -= adjustment;
+ *offset -= adjustment;
}

Powered by Google App Engine
This is Rietveld 408576698