diff options
author | Lei Zhang <thestig@chromium.org> | 2017-11-21 18:07:00 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-11-21 18:07:00 +0000 |
commit | 135c6601bd9d12bd935371d3122f8e75391de75b (patch) | |
tree | 185227ece2aa6cceb58911d2b25871dab4860251 /core/fxcrt/widestring.cpp | |
parent | f276e78e190e04b25c3f05b35a28ecdd30bdcf21 (diff) | |
download | pdfium-135c6601bd9d12bd935371d3122f8e75391de75b.tar.xz |
Add {Byte/Wide}String::Trim().
So callers no longer have to call str.TrimLeft(); str.TrimRight();
Change-Id: If931eaa8d891f487b9618ebbeee5ee294e807846
Reviewed-on: https://pdfium-review.googlesource.com/18990
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Diffstat (limited to 'core/fxcrt/widestring.cpp')
-rw-r--r-- | core/fxcrt/widestring.cpp | 72 |
1 files changed, 45 insertions, 27 deletions
diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp index d33ed1ad6c..2caa3b4213 100644 --- a/core/fxcrt/widestring.cpp +++ b/core/fxcrt/widestring.cpp @@ -31,6 +31,8 @@ template struct std::hash<WideString>; namespace { +constexpr wchar_t kTrimChars[] = L"\x09\x0a\x0b\x0c\x0d\x20"; + const wchar_t* FX_wcsstr(const wchar_t* haystack, int haystack_len, const wchar_t* needle, @@ -958,32 +960,33 @@ size_t WideString::WStringLength(const unsigned short* str) { return len; } -void WideString::TrimRight(const WideStringView& pTargets) { - if (IsEmpty() || pTargets.IsEmpty()) - return; +void WideString::Trim() { + TrimRight(kTrimChars); + TrimLeft(kTrimChars); +} - size_t pos = GetLength(); - while (pos && pTargets.Contains(m_pData->m_String[pos - 1])) - pos--; +void WideString::Trim(wchar_t target) { + wchar_t str[2] = {target, 0}; + TrimRight(str); + TrimLeft(str); +} - if (pos < m_pData->m_nDataLength) { - ReallocBeforeWrite(m_pData->m_nDataLength); - m_pData->m_String[pos] = 0; - m_pData->m_nDataLength = pos; - } +void WideString::Trim(const WideStringView& targets) { + TrimRight(targets); + TrimLeft(targets); } -void WideString::TrimRight(wchar_t chTarget) { - wchar_t str[2] = {chTarget, 0}; - TrimRight(str); +void WideString::TrimLeft() { + TrimLeft(kTrimChars); } -void WideString::TrimRight() { - TrimRight(L"\x09\x0a\x0b\x0c\x0d\x20"); +void WideString::TrimLeft(wchar_t target) { + wchar_t str[2] = {target, 0}; + TrimLeft(str); } -void WideString::TrimLeft(const WideStringView& pTargets) { - if (!m_pData || pTargets.IsEmpty()) +void WideString::TrimLeft(const WideStringView& targets) { + if (!m_pData || targets.IsEmpty()) return; size_t len = GetLength(); @@ -993,13 +996,12 @@ void WideString::TrimLeft(const WideStringView& pTargets) { size_t pos = 0; while (pos < len) { size_t i = 0; - while (i < pTargets.GetLength() && - pTargets.CharAt(i) != m_pData->m_String[pos]) { + while (i < targets.GetLength() && + targets.CharAt(i) != m_pData->m_String[pos]) { i++; } - if (i == pTargets.GetLength()) { + if (i == targets.GetLength()) break; - } pos++; } if (!pos) @@ -1012,14 +1014,30 @@ void WideString::TrimLeft(const WideStringView& pTargets) { m_pData->m_nDataLength = nDataLength; } -void WideString::TrimLeft(wchar_t chTarget) { - wchar_t str[2] = {chTarget, 0}; - TrimLeft(str); +void WideString::TrimRight() { + TrimRight(kTrimChars); } -void WideString::TrimLeft() { - TrimLeft(L"\x09\x0a\x0b\x0c\x0d\x20"); +void WideString::TrimRight(wchar_t target) { + wchar_t str[2] = {target, 0}; + TrimRight(str); } + +void WideString::TrimRight(const WideStringView& targets) { + if (IsEmpty() || targets.IsEmpty()) + return; + + size_t pos = GetLength(); + while (pos && targets.Contains(m_pData->m_String[pos - 1])) + pos--; + + if (pos < m_pData->m_nDataLength) { + ReallocBeforeWrite(m_pData->m_nDataLength); + m_pData->m_String[pos] = 0; + m_pData->m_nDataLength = pos; + } +} + float FX_wtof(const wchar_t* str, int len) { if (len == 0) { return 0.0; |