1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include <stddef.h> // For offsetof().
9 #include "../../include/fxcrt/fx_basic.h"
10 #include "../../../third_party/base/numerics/safe_math.h"
12 static int _Buffer_itoa(char* buf, int i, FX_DWORD flags) {
20 if ((flags & FXFORMAT_SIGNED) && i < 0) {
24 const FX_CHAR* string = "0123456789abcdef";
25 if (flags & FXFORMAT_HEX) {
27 if (flags & FXFORMAT_CAPITAL) {
28 string = "0123456789ABCDEF";
32 buf1[buf_pos--] = string[u % base];
35 if ((flags & FXFORMAT_SIGNED) && i < 0) {
36 buf1[buf_pos--] = '-';
38 int len = 31 - buf_pos;
39 for (int ii = 0; ii < len; ii++) {
40 buf[ii] = buf1[ii + buf_pos + 1];
44 CFX_ByteString CFX_ByteString::FormatInteger(int i, FX_DWORD flags) {
46 return CFX_ByteStringC(buf, _Buffer_itoa(buf, i, flags));
50 CFX_ByteString::StringData* CFX_ByteString::StringData::Create(int nLen) {
51 // |nLen| is currently declared as in |int|. TODO(palmer): It should be
52 // a |size_t|, or at least unsigned.
53 if (nLen == 0 || nLen < 0) {
57 // Fixed portion of header plus a NUL char not included in m_nAllocLength.
58 // sizeof(FX_CHAR) is always 1, used for consistency with CFX_Widestring.
59 int overhead = offsetof(StringData, m_String) + sizeof(FX_CHAR);
60 pdfium::base::CheckedNumeric<int> nSize = nLen;
63 // Now round to an 8-byte boundary. We'd expect that this is the minimum
64 // granularity of any of the underlying allocators, so there may be cases
65 // where we can save a re-alloc when adding a few characters to a string
66 // by using this otherwise wasted space.
68 int totalSize = nSize.ValueOrDie() & ~7;
69 int usableSize = totalSize - overhead;
70 FXSYS_assert(usableSize >= nLen);
72 void* pData = FX_Alloc(uint8_t, totalSize);
73 return new (pData) StringData(nLen, usableSize);
75 CFX_ByteString::~CFX_ByteString() {
80 CFX_ByteString::CFX_ByteString(const FX_CHAR* lpsz, FX_STRSIZE nLen) {
82 nLen = lpsz ? FXSYS_strlen(lpsz) : 0;
85 m_pData = StringData::Create(nLen);
87 FXSYS_memcpy(m_pData->m_String, lpsz, nLen);
93 CFX_ByteString::CFX_ByteString(const uint8_t* lpsz, FX_STRSIZE nLen) {
95 m_pData = StringData::Create(nLen);
97 FXSYS_memcpy(m_pData->m_String, lpsz, nLen);
103 CFX_ByteString::CFX_ByteString(char ch) {
104 m_pData = StringData::Create(1);
106 m_pData->m_String[0] = ch;
109 CFX_ByteString::CFX_ByteString(const CFX_ByteString& stringSrc) {
110 if (stringSrc.m_pData == NULL) {
114 if (stringSrc.m_pData->m_nRefs >= 0) {
115 m_pData = stringSrc.m_pData;
122 CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& stringSrc) {
123 if (stringSrc.IsEmpty()) {
130 CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& str1,
131 const CFX_ByteStringC& str2) {
133 int nNewLen = str1.GetLength() + str2.GetLength();
137 m_pData = StringData::Create(nNewLen);
139 FXSYS_memcpy(m_pData->m_String, str1.GetCStr(), str1.GetLength());
140 FXSYS_memcpy(m_pData->m_String + str1.GetLength(), str2.GetCStr(),
144 const CFX_ByteString& CFX_ByteString::operator=(const FX_CHAR* lpsz) {
145 if (lpsz == NULL || lpsz[0] == 0) {
148 AssignCopy(FXSYS_strlen(lpsz), lpsz);
152 const CFX_ByteString& CFX_ByteString::operator=(const CFX_ByteStringC& str) {
156 AssignCopy(str.GetLength(), str.GetCStr());
160 const CFX_ByteString& CFX_ByteString::operator=(
161 const CFX_ByteString& stringSrc) {
162 if (m_pData == stringSrc.m_pData) {
165 if (stringSrc.IsEmpty()) {
167 } else if ((m_pData && m_pData->m_nRefs < 0) ||
168 (stringSrc.m_pData && stringSrc.m_pData->m_nRefs < 0)) {
169 AssignCopy(stringSrc.m_pData->m_nDataLength, stringSrc.m_pData->m_String);
172 m_pData = stringSrc.m_pData;
179 const CFX_ByteString& CFX_ByteString::operator=(const CFX_BinaryBuf& buf) {
180 Load(buf.GetBuffer(), buf.GetSize());
183 void CFX_ByteString::Load(const uint8_t* buf, FX_STRSIZE len) {
186 m_pData = StringData::Create(len);
188 FXSYS_memcpy(m_pData->m_String, buf, len);
194 const CFX_ByteString& CFX_ByteString::operator+=(const FX_CHAR* lpsz) {
196 ConcatInPlace(FXSYS_strlen(lpsz), lpsz);
200 const CFX_ByteString& CFX_ByteString::operator+=(char ch) {
201 ConcatInPlace(1, &ch);
204 const CFX_ByteString& CFX_ByteString::operator+=(const CFX_ByteString& string) {
205 if (string.m_pData == NULL) {
208 ConcatInPlace(string.m_pData->m_nDataLength, string.m_pData->m_String);
211 const CFX_ByteString& CFX_ByteString::operator+=(
212 const CFX_ByteStringC& string) {
213 if (string.IsEmpty()) {
216 ConcatInPlace(string.GetLength(), string.GetCStr());
219 bool CFX_ByteString::Equal(const char* ptr) const {
221 return !ptr || ptr[0] == '\0';
224 return m_pData->m_nDataLength == 0;
226 return FXSYS_strlen(ptr) == m_pData->m_nDataLength &&
227 FXSYS_memcmp(ptr, m_pData->m_String, m_pData->m_nDataLength) == 0;
229 bool CFX_ByteString::Equal(const CFX_ByteStringC& str) const {
230 if (m_pData == NULL) {
231 return str.IsEmpty();
233 return m_pData->m_nDataLength == str.GetLength() &&
234 FXSYS_memcmp(m_pData->m_String, str.GetCStr(), str.GetLength()) == 0;
236 bool CFX_ByteString::Equal(const CFX_ByteString& other) const {
238 return other.IsEmpty();
240 if (other.IsEmpty()) {
243 return other.m_pData->m_nDataLength == m_pData->m_nDataLength &&
244 FXSYS_memcmp(other.m_pData->m_String, m_pData->m_String,
245 m_pData->m_nDataLength) == 0;
247 void CFX_ByteString::Empty() {
253 bool CFX_ByteString::EqualNoCase(const CFX_ByteStringC& str) const {
254 if (m_pData == NULL) {
255 return str.IsEmpty();
257 FX_STRSIZE len = str.GetLength();
258 if (m_pData->m_nDataLength != len) {
261 const uint8_t* pThis = (const uint8_t*)m_pData->m_String;
262 const uint8_t* pThat = str.GetPtr();
263 for (FX_STRSIZE i = 0; i < len; i++) {
264 if ((*pThis) != (*pThat)) {
265 uint8_t bThis = *pThis;
266 if (bThis >= 'A' && bThis <= 'Z') {
269 uint8_t bThat = *pThat;
270 if (bThat >= 'A' && bThat <= 'Z') {
273 if (bThis != bThat) {
282 void CFX_ByteString::AssignCopy(FX_STRSIZE nSrcLen,
283 const FX_CHAR* lpszSrcData) {
284 AllocBeforeWrite(nSrcLen);
285 FXSYS_memcpy(m_pData->m_String, lpszSrcData, nSrcLen);
286 m_pData->m_nDataLength = nSrcLen;
287 m_pData->m_String[nSrcLen] = 0;
289 void CFX_ByteString::CopyBeforeWrite() {
290 if (m_pData == NULL || m_pData->m_nRefs <= 1) {
293 StringData* pData = m_pData;
295 FX_STRSIZE nDataLength = pData->m_nDataLength;
296 m_pData = StringData::Create(nDataLength);
297 if (m_pData != NULL) {
298 FXSYS_memcpy(m_pData->m_String, pData->m_String, nDataLength + 1);
301 void CFX_ByteString::AllocBeforeWrite(FX_STRSIZE nLen) {
302 if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) {
306 m_pData = StringData::Create(nLen);
308 void CFX_ByteString::ReleaseBuffer(FX_STRSIZE nNewLength) {
309 if (m_pData == NULL) {
313 if (nNewLength == -1) {
314 nNewLength = FXSYS_strlen((const FX_CHAR*)m_pData->m_String);
316 if (nNewLength == 0) {
320 FXSYS_assert(nNewLength <= m_pData->m_nAllocLength);
321 m_pData->m_nDataLength = nNewLength;
322 m_pData->m_String[nNewLength] = 0;
324 void CFX_ByteString::Reserve(FX_STRSIZE len) {
326 ReleaseBuffer(GetLength());
328 FX_CHAR* CFX_ByteString::GetBuffer(FX_STRSIZE nMinBufLength) {
329 if (m_pData == NULL && nMinBufLength == 0) {
332 if (m_pData && m_pData->m_nRefs <= 1 &&
333 m_pData->m_nAllocLength >= nMinBufLength) {
334 return m_pData->m_String;
336 if (m_pData == NULL) {
337 m_pData = StringData::Create(nMinBufLength);
341 m_pData->m_nDataLength = 0;
342 m_pData->m_String[0] = 0;
343 return m_pData->m_String;
345 StringData* pOldData = m_pData;
346 FX_STRSIZE nOldLen = pOldData->m_nDataLength;
347 if (nMinBufLength < nOldLen) {
348 nMinBufLength = nOldLen;
350 m_pData = StringData::Create(nMinBufLength);
354 FXSYS_memcpy(m_pData->m_String, pOldData->m_String, (nOldLen + 1));
355 m_pData->m_nDataLength = nOldLen;
357 return m_pData->m_String;
359 FX_STRSIZE CFX_ByteString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount) {
360 if (m_pData == NULL) {
366 FX_STRSIZE nOldLength = m_pData->m_nDataLength;
367 if (nCount > 0 && nIndex < nOldLength) {
368 FX_STRSIZE mLength = nIndex + nCount;
369 if (mLength >= nOldLength) {
370 m_pData->m_nDataLength = nIndex;
371 return m_pData->m_nDataLength;
374 int nBytesToCopy = nOldLength - mLength + 1;
375 FXSYS_memmove(m_pData->m_String + nIndex, m_pData->m_String + mLength,
377 m_pData->m_nDataLength = nOldLength - nCount;
379 return m_pData->m_nDataLength;
381 void CFX_ByteString::ConcatInPlace(FX_STRSIZE nSrcLen,
382 const FX_CHAR* lpszSrcData) {
383 if (nSrcLen == 0 || lpszSrcData == NULL) {
386 if (m_pData == NULL) {
387 m_pData = StringData::Create(nSrcLen);
391 FXSYS_memcpy(m_pData->m_String, lpszSrcData, nSrcLen);
394 if (m_pData->m_nRefs > 1 ||
395 m_pData->m_nDataLength + nSrcLen > m_pData->m_nAllocLength) {
396 ConcatCopy(m_pData->m_nDataLength, m_pData->m_String, nSrcLen, lpszSrcData);
398 FXSYS_memcpy(m_pData->m_String + m_pData->m_nDataLength, lpszSrcData,
400 m_pData->m_nDataLength += nSrcLen;
401 m_pData->m_String[m_pData->m_nDataLength] = 0;
404 void CFX_ByteString::ConcatCopy(FX_STRSIZE nSrc1Len,
405 const FX_CHAR* lpszSrc1Data,
407 const FX_CHAR* lpszSrc2Data) {
408 int nNewLen = nSrc1Len + nSrc2Len;
412 // Don't release until done copying, might be one of the arguments.
413 StringData* pOldData = m_pData;
414 m_pData = StringData::Create(nNewLen);
416 memcpy(m_pData->m_String, lpszSrc1Data, nSrc1Len);
417 memcpy(m_pData->m_String + nSrc1Len, lpszSrc2Data, nSrc2Len);
421 CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst) const {
422 if (m_pData == NULL) {
423 return CFX_ByteString();
425 return Mid(nFirst, m_pData->m_nDataLength - nFirst);
427 CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const {
434 if (nFirst + nCount > m_pData->m_nDataLength) {
435 nCount = m_pData->m_nDataLength - nFirst;
437 if (nFirst > m_pData->m_nDataLength) {
440 if (nFirst == 0 && nFirst + nCount == m_pData->m_nDataLength) {
444 AllocCopy(dest, nCount, nFirst);
447 void CFX_ByteString::AllocCopy(CFX_ByteString& dest,
449 FX_STRSIZE nCopyIndex) const {
450 // |FX_STRSIZE| is currently typedef'd as in |int|. TODO(palmer): It
451 // should be a |size_t|, or at least unsigned.
452 if (nCopyLen == 0 || nCopyLen < 0) {
455 ASSERT(dest.m_pData == NULL);
456 dest.m_pData = StringData::Create(nCopyLen);
458 FXSYS_memcpy(dest.m_pData->m_String, m_pData->m_String + nCopyIndex,
462 #define FORCE_ANSI 0x10000
463 #define FORCE_UNICODE 0x20000
464 #define FORCE_INT64 0x40000
465 void CFX_ByteString::FormatV(const FX_CHAR* lpszFormat, va_list argList) {
467 #if defined(__ARMCC_VERSION) || \
468 (!defined(_MSC_VER) && (_FX_CPU_ == _FX_X64_ || _FX_CPU_ == _FX_IA64_ || \
469 _FX_CPU_ == _FX_ARM64_)) || \
470 defined(__native_client__)
471 va_copy(argListSave, argList);
473 argListSave = argList;
476 for (const FX_CHAR* lpsz = lpszFormat; *lpsz != 0; lpsz++) {
477 if (*lpsz != '%' || *(lpsz = lpsz + 1) == '%') {
478 nMaxLen += FXSYS_strlen(lpsz);
483 for (; *lpsz != 0; lpsz++) {
486 } else if (*lpsz == '*') {
487 nWidth = va_arg(argList, int);
488 } else if (*lpsz == '-' || *lpsz == '+' || *lpsz == '0' || *lpsz == ' ')
495 nWidth = FXSYS_atoi(lpsz);
496 for (; (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz++)
499 if (nWidth < 0 || nWidth > 128 * 1024) {
500 lpszFormat = "Bad width";
508 nPrecision = va_arg(argList, int);
511 nPrecision = FXSYS_atoi(lpsz);
512 for (; (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz++)
516 if (nPrecision < 0 || nPrecision > 128 * 1024) {
517 lpszFormat = "Bad precision";
522 if (FXSYS_strncmp(lpsz, "I64", 3) == 0) {
524 nModifier = FORCE_INT64;
528 nModifier = FORCE_ANSI;
532 nModifier = FORCE_UNICODE;
542 switch (*lpsz | nModifier) {
546 va_arg(argList, int);
548 case 'c' | FORCE_ANSI:
549 case 'C' | FORCE_ANSI:
551 va_arg(argList, int);
553 case 'c' | FORCE_UNICODE:
554 case 'C' | FORCE_UNICODE:
556 va_arg(argList, int);
559 const FX_CHAR* pstrNextArg = va_arg(argList, const FX_CHAR*);
560 if (pstrNextArg == NULL) {
563 nItemLen = FXSYS_strlen(pstrNextArg);
570 FX_WCHAR* pstrNextArg = va_arg(argList, FX_WCHAR*);
571 if (pstrNextArg == NULL) {
574 nItemLen = FXSYS_wcslen(pstrNextArg);
580 case 's' | FORCE_ANSI:
581 case 'S' | FORCE_ANSI: {
582 const FX_CHAR* pstrNextArg = va_arg(argList, const FX_CHAR*);
583 if (pstrNextArg == NULL) {
586 nItemLen = FXSYS_strlen(pstrNextArg);
592 case 's' | FORCE_UNICODE:
593 case 'S' | FORCE_UNICODE: {
594 FX_WCHAR* pstrNextArg = va_arg(argList, FX_WCHAR*);
595 if (pstrNextArg == NULL) {
598 nItemLen = FXSYS_wcslen(pstrNextArg);
606 if (nPrecision != 0 && nItemLen > nPrecision) {
607 nItemLen = nPrecision;
609 if (nItemLen < nWidth) {
620 if (nModifier & FORCE_INT64) {
621 va_arg(argList, int64_t);
623 va_arg(argList, int);
626 if (nItemLen < nWidth + nPrecision) {
627 nItemLen = nWidth + nPrecision;
636 va_arg(argList, double);
638 if (nItemLen < nWidth + nPrecision) {
639 nItemLen = nWidth + nPrecision;
643 if (nWidth + nPrecision > 100) {
644 nItemLen = nPrecision + nWidth + 128;
647 double f = va_arg(argList, double);
648 memset(pszTemp, 0, sizeof(pszTemp));
649 FXSYS_snprintf(pszTemp, sizeof(pszTemp) - 1, "%*.*f", nWidth,
651 nItemLen = FXSYS_strlen(pszTemp);
655 va_arg(argList, void*);
657 if (nItemLen < nWidth + nPrecision) {
658 nItemLen = nWidth + nPrecision;
662 va_arg(argList, int*);
668 nMaxLen += 32; // Fudge factor.
671 memset(m_pData->m_String, 0, nMaxLen);
672 FXSYS_vsnprintf(m_pData->m_String, nMaxLen - 1, lpszFormat, argListSave);
677 void CFX_ByteString::Format(const FX_CHAR* lpszFormat, ...) {
679 va_start(argList, lpszFormat);
680 FormatV(lpszFormat, argList);
683 FX_STRSIZE CFX_ByteString::Insert(FX_STRSIZE nIndex, FX_CHAR ch) {
688 FX_STRSIZE nNewLength = m_pData ? m_pData->m_nDataLength : 0;
689 if (nIndex > nNewLength) {
693 if (m_pData == NULL || m_pData->m_nAllocLength < nNewLength) {
694 StringData* pOldData = m_pData;
695 const FX_CHAR* pstr = m_pData->m_String;
696 m_pData = StringData::Create(nNewLength);
700 if (pOldData != NULL) {
701 FXSYS_memmove(m_pData->m_String, pstr, (pOldData->m_nDataLength + 1));
704 m_pData->m_String[0] = 0;
707 FXSYS_memmove(m_pData->m_String + nIndex + 1, m_pData->m_String + nIndex,
708 (nNewLength - nIndex));
709 m_pData->m_String[nIndex] = ch;
710 m_pData->m_nDataLength = nNewLength;
713 CFX_ByteString CFX_ByteString::Right(FX_STRSIZE nCount) const {
714 if (m_pData == NULL) {
715 return CFX_ByteString();
720 if (nCount >= m_pData->m_nDataLength) {
724 AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount);
727 CFX_ByteString CFX_ByteString::Left(FX_STRSIZE nCount) const {
728 if (m_pData == NULL) {
729 return CFX_ByteString();
734 if (nCount >= m_pData->m_nDataLength) {
738 AllocCopy(dest, nCount, 0);
741 FX_STRSIZE CFX_ByteString::Find(FX_CHAR ch, FX_STRSIZE nStart) const {
742 if (m_pData == NULL) {
745 FX_STRSIZE nLength = m_pData->m_nDataLength;
746 if (nStart >= nLength) {
749 const FX_CHAR* lpsz = FXSYS_strchr(m_pData->m_String + nStart, ch);
750 return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String);
752 FX_STRSIZE CFX_ByteString::ReverseFind(FX_CHAR ch) const {
753 if (m_pData == NULL) {
756 FX_STRSIZE nLength = m_pData->m_nDataLength;
758 if (m_pData->m_String[nLength - 1] == ch) {
765 const FX_CHAR* FX_strstr(const FX_CHAR* str1,
769 if (len2 > len1 || len2 == 0) {
772 const FX_CHAR* end_ptr = str1 + len1 - len2;
773 while (str1 <= end_ptr) {
776 if (str1[i] != str2[i]) {
788 FX_STRSIZE CFX_ByteString::Find(const CFX_ByteStringC& lpszSub,
789 FX_STRSIZE nStart) const {
790 if (m_pData == NULL) {
793 FX_STRSIZE nLength = m_pData->m_nDataLength;
794 if (nStart > nLength) {
797 const FX_CHAR* lpsz =
798 FX_strstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart,
799 lpszSub.GetCStr(), lpszSub.GetLength());
800 return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String);
802 void CFX_ByteString::MakeLower() {
803 if (m_pData == NULL) {
807 if (GetLength() < 1) {
810 FXSYS_strlwr(m_pData->m_String);
812 void CFX_ByteString::MakeUpper() {
813 if (m_pData == NULL) {
817 if (GetLength() < 1) {
820 FXSYS_strupr(m_pData->m_String);
822 FX_STRSIZE CFX_ByteString::Remove(FX_CHAR chRemove) {
823 if (m_pData == NULL) {
827 if (GetLength() < 1) {
830 FX_CHAR* pstrSource = m_pData->m_String;
831 FX_CHAR* pstrDest = m_pData->m_String;
832 FX_CHAR* pstrEnd = m_pData->m_String + m_pData->m_nDataLength;
833 while (pstrSource < pstrEnd) {
834 if (*pstrSource != chRemove) {
835 *pstrDest = *pstrSource;
841 FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest);
842 m_pData->m_nDataLength -= nCount;
845 FX_STRSIZE CFX_ByteString::Replace(const CFX_ByteStringC& lpszOld,
846 const CFX_ByteStringC& lpszNew) {
847 if (m_pData == NULL) {
850 if (lpszOld.IsEmpty()) {
853 FX_STRSIZE nSourceLen = lpszOld.GetLength();
854 FX_STRSIZE nReplacementLen = lpszNew.GetLength();
855 FX_STRSIZE nCount = 0;
856 const FX_CHAR* pStart = m_pData->m_String;
857 FX_CHAR* pEnd = m_pData->m_String + m_pData->m_nDataLength;
859 const FX_CHAR* pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart),
860 lpszOld.GetCStr(), nSourceLen);
861 if (pTarget == NULL) {
865 pStart = pTarget + nSourceLen;
870 FX_STRSIZE nNewLength =
871 m_pData->m_nDataLength + (nReplacementLen - nSourceLen) * nCount;
872 if (nNewLength == 0) {
876 StringData* pNewData = StringData::Create(nNewLength);
880 pStart = m_pData->m_String;
881 FX_CHAR* pDest = pNewData->m_String;
882 for (FX_STRSIZE i = 0; i < nCount; i++) {
883 const FX_CHAR* pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart),
884 lpszOld.GetCStr(), nSourceLen);
885 FXSYS_memcpy(pDest, pStart, pTarget - pStart);
886 pDest += pTarget - pStart;
887 FXSYS_memcpy(pDest, lpszNew.GetCStr(), lpszNew.GetLength());
888 pDest += lpszNew.GetLength();
889 pStart = pTarget + nSourceLen;
891 FXSYS_memcpy(pDest, pStart, pEnd - pStart);
896 void CFX_ByteString::SetAt(FX_STRSIZE nIndex, FX_CHAR ch) {
897 if (m_pData == NULL) {
900 FXSYS_assert(nIndex >= 0);
901 FXSYS_assert(nIndex < m_pData->m_nDataLength);
903 m_pData->m_String[nIndex] = ch;
905 CFX_WideString CFX_ByteString::UTF8Decode() const {
906 CFX_UTF8Decoder decoder;
907 for (FX_STRSIZE i = 0; i < GetLength(); i++) {
908 decoder.Input((uint8_t)m_pData->m_String[i]);
910 return decoder.GetResult();
912 CFX_ByteString CFX_ByteString::FromUnicode(const FX_WCHAR* str,
915 len = FXSYS_wcslen(str);
918 bstr.ConvertFrom(CFX_WideString(str, len));
921 CFX_ByteString CFX_ByteString::FromUnicode(const CFX_WideString& str) {
922 return FromUnicode(str.c_str(), str.GetLength());
924 void CFX_ByteString::ConvertFrom(const CFX_WideString& str,
925 CFX_CharMap* pCharMap) {
926 if (pCharMap == NULL) {
927 pCharMap = CFX_CharMap::GetDefaultMapper();
929 *this = (*pCharMap->m_GetByteString)(pCharMap, str);
931 int CFX_ByteString::Compare(const CFX_ByteStringC& str) const {
932 if (m_pData == NULL) {
933 return str.IsEmpty() ? 0 : -1;
935 int this_len = m_pData->m_nDataLength;
936 int that_len = str.GetLength();
937 int min_len = this_len < that_len ? this_len : that_len;
938 for (int i = 0; i < min_len; i++) {
939 if ((uint8_t)m_pData->m_String[i] < str.GetAt(i)) {
942 if ((uint8_t)m_pData->m_String[i] > str.GetAt(i)) {
946 if (this_len < that_len) {
949 if (this_len > that_len) {
954 void CFX_ByteString::TrimRight(const CFX_ByteStringC& lpszTargets) {
955 if (m_pData == NULL || lpszTargets.IsEmpty()) {
959 FX_STRSIZE pos = GetLength();
965 while (i < lpszTargets.GetLength() &&
966 lpszTargets[i] != m_pData->m_String[pos - 1]) {
969 if (i == lpszTargets.GetLength()) {
974 if (pos < m_pData->m_nDataLength) {
975 m_pData->m_String[pos] = 0;
976 m_pData->m_nDataLength = pos;
979 void CFX_ByteString::TrimRight(FX_CHAR chTarget) {
980 TrimRight(CFX_ByteStringC(chTarget));
982 void CFX_ByteString::TrimRight() {
983 TrimRight(FX_BSTRC("\x09\x0a\x0b\x0c\x0d\x20"));
985 void CFX_ByteString::TrimLeft(const CFX_ByteStringC& lpszTargets) {
986 if (m_pData == NULL) {
989 if (lpszTargets.IsEmpty()) {
993 FX_STRSIZE len = GetLength();
1000 while (i < lpszTargets.GetLength() &&
1001 lpszTargets[i] != m_pData->m_String[pos]) {
1004 if (i == lpszTargets.GetLength()) {
1010 FX_STRSIZE nDataLength = len - pos;
1011 FXSYS_memmove(m_pData->m_String, m_pData->m_String + pos,
1012 (nDataLength + 1) * sizeof(FX_CHAR));
1013 m_pData->m_nDataLength = nDataLength;
1016 void CFX_ByteString::TrimLeft(FX_CHAR chTarget) {
1017 TrimLeft(CFX_ByteStringC(chTarget));
1019 void CFX_ByteString::TrimLeft() {
1020 TrimLeft(FX_BSTRC("\x09\x0a\x0b\x0c\x0d\x20"));
1022 FX_DWORD CFX_ByteString::GetID(FX_STRSIZE start_pos) const {
1023 return CFX_ByteStringC(*this).GetID(start_pos);
1025 FX_DWORD CFX_ByteStringC::GetID(FX_STRSIZE start_pos) const {
1026 if (m_Length == 0) {
1029 if (start_pos < 0 || start_pos >= m_Length) {
1033 if (start_pos + 4 > m_Length) {
1034 for (FX_STRSIZE i = 0; i < m_Length - start_pos; i++) {
1035 strid = strid * 256 + m_Ptr[start_pos + i];
1037 strid = strid << ((4 - m_Length + start_pos) * 8);
1039 for (int i = 0; i < 4; i++) {
1040 strid = strid * 256 + m_Ptr[start_pos + i];
1045 FX_STRSIZE FX_ftoa(FX_FLOAT d, FX_CHAR* buf) {
1051 FX_BOOL bNegative = FALSE;
1057 int scaled = FXSYS_round(d);
1058 while (scaled < 100000) {
1059 if (scale == 1000000) {
1063 scaled = FXSYS_round(d * scale);
1071 buf[buf_size++] = '-';
1073 int i = scaled / scale;
1074 FXSYS_itoa(i, buf2, 10);
1075 FX_STRSIZE len = FXSYS_strlen(buf2);
1076 FXSYS_memcpy(buf + buf_size, buf2, len);
1078 int fraction = scaled % scale;
1079 if (fraction == 0) {
1082 buf[buf_size++] = '.';
1085 buf[buf_size++] = '0' + fraction / scale;
1091 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) {
1093 FX_STRSIZE len = FX_ftoa(d, buf);
1094 return CFX_ByteString(buf, len);