OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
| 9 #include <algorithm> |
9 #include <cctype> | 10 #include <cctype> |
10 | 11 |
11 #include "core/fxcrt/include/fx_basic.h" | 12 #include "core/fxcrt/include/fx_basic.h" |
12 #include "third_party/base/numerics/safe_math.h" | 13 #include "third_party/base/numerics/safe_math.h" |
13 | 14 |
14 template class CFX_StringDataTemplate<FX_CHAR>; | 15 template class CFX_StringDataTemplate<FX_CHAR>; |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 int Buffer_itoa(char* buf, int i, uint32_t flags) { | 19 int Buffer_itoa(char* buf, int i, uint32_t flags) { |
(...skipping 22 matching lines...) Expand all Loading... |
41 if ((flags & FXFORMAT_SIGNED) && i < 0) { | 42 if ((flags & FXFORMAT_SIGNED) && i < 0) { |
42 buf1[buf_pos--] = '-'; | 43 buf1[buf_pos--] = '-'; |
43 } | 44 } |
44 int len = 31 - buf_pos; | 45 int len = 31 - buf_pos; |
45 for (int ii = 0; ii < len; ii++) { | 46 for (int ii = 0; ii < len; ii++) { |
46 buf[ii] = buf1[ii + buf_pos + 1]; | 47 buf[ii] = buf1[ii + buf_pos + 1]; |
47 } | 48 } |
48 return len; | 49 return len; |
49 } | 50 } |
50 | 51 |
| 52 const FX_CHAR* FX_strstr(const FX_CHAR* haystack, |
| 53 int haystack_len, |
| 54 const FX_CHAR* needle, |
| 55 int needle_len) { |
| 56 if (needle_len > haystack_len || needle_len == 0) { |
| 57 return nullptr; |
| 58 } |
| 59 const FX_CHAR* end_ptr = haystack + haystack_len - needle_len; |
| 60 while (haystack <= end_ptr) { |
| 61 int i = 0; |
| 62 while (1) { |
| 63 if (haystack[i] != needle[i]) { |
| 64 break; |
| 65 } |
| 66 i++; |
| 67 if (i == needle_len) { |
| 68 return haystack; |
| 69 } |
| 70 } |
| 71 haystack++; |
| 72 } |
| 73 return nullptr; |
| 74 } |
| 75 |
51 } // namespace | 76 } // namespace |
52 | 77 |
53 CFX_ByteString::CFX_ByteString(const FX_CHAR* pStr, FX_STRSIZE nLen) { | 78 CFX_ByteString::CFX_ByteString(const FX_CHAR* pStr, FX_STRSIZE nLen) { |
54 if (nLen < 0) | 79 if (nLen < 0) |
55 nLen = pStr ? FXSYS_strlen(pStr) : 0; | 80 nLen = pStr ? FXSYS_strlen(pStr) : 0; |
56 | 81 |
57 if (nLen) | 82 if (nLen) |
58 m_pData.Reset(StringData::Create(pStr, nLen)); | 83 m_pData.Reset(StringData::Create(pStr, nLen)); |
59 } | 84 } |
60 | 85 |
(...skipping 30 matching lines...) Expand all Loading... |
91 | 116 |
92 const CFX_ByteString& CFX_ByteString::operator=(const FX_CHAR* pStr) { | 117 const CFX_ByteString& CFX_ByteString::operator=(const FX_CHAR* pStr) { |
93 if (!pStr || !pStr[0]) | 118 if (!pStr || !pStr[0]) |
94 clear(); | 119 clear(); |
95 else | 120 else |
96 AssignCopy(pStr, FXSYS_strlen(pStr)); | 121 AssignCopy(pStr, FXSYS_strlen(pStr)); |
97 | 122 |
98 return *this; | 123 return *this; |
99 } | 124 } |
100 | 125 |
101 const CFX_ByteString& CFX_ByteString::operator=(const CFX_ByteStringC& str) { | 126 const CFX_ByteString& CFX_ByteString::operator=( |
102 if (str.IsEmpty()) | 127 const CFX_ByteStringC& stringSrc) { |
| 128 if (stringSrc.IsEmpty()) |
103 clear(); | 129 clear(); |
104 else | 130 else |
105 AssignCopy(str.c_str(), str.GetLength()); | 131 AssignCopy(stringSrc.c_str(), stringSrc.GetLength()); |
106 | 132 |
107 return *this; | 133 return *this; |
108 } | 134 } |
109 | 135 |
110 const CFX_ByteString& CFX_ByteString::operator=( | 136 const CFX_ByteString& CFX_ByteString::operator=( |
111 const CFX_ByteString& stringSrc) { | 137 const CFX_ByteString& stringSrc) { |
112 if (m_pData != stringSrc.m_pData) | 138 if (m_pData != stringSrc.m_pData) |
113 m_pData = stringSrc.m_pData; | 139 m_pData = stringSrc.m_pData; |
114 | 140 |
115 return *this; | 141 return *this; |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 nIndex = 0; | 336 nIndex = 0; |
311 | 337 |
312 FX_STRSIZE nOldLength = m_pData->m_nDataLength; | 338 FX_STRSIZE nOldLength = m_pData->m_nDataLength; |
313 if (nCount > 0 && nIndex < nOldLength) { | 339 if (nCount > 0 && nIndex < nOldLength) { |
314 FX_STRSIZE mLength = nIndex + nCount; | 340 FX_STRSIZE mLength = nIndex + nCount; |
315 if (mLength >= nOldLength) { | 341 if (mLength >= nOldLength) { |
316 m_pData->m_nDataLength = nIndex; | 342 m_pData->m_nDataLength = nIndex; |
317 return m_pData->m_nDataLength; | 343 return m_pData->m_nDataLength; |
318 } | 344 } |
319 ReallocBeforeWrite(nOldLength); | 345 ReallocBeforeWrite(nOldLength); |
320 int nBytesToCopy = nOldLength - mLength + 1; | 346 int nCharsToCopy = nOldLength - mLength + 1; |
321 FXSYS_memmove(m_pData->m_String + nIndex, m_pData->m_String + mLength, | 347 FXSYS_memmove(m_pData->m_String + nIndex, m_pData->m_String + mLength, |
322 nBytesToCopy); | 348 nCharsToCopy); |
323 m_pData->m_nDataLength = nOldLength - nCount; | 349 m_pData->m_nDataLength = nOldLength - nCount; |
324 } | 350 } |
325 return m_pData->m_nDataLength; | 351 return m_pData->m_nDataLength; |
326 } | 352 } |
327 | 353 |
328 void CFX_ByteString::Concat(const FX_CHAR* pSrcData, FX_STRSIZE nSrcLen) { | 354 void CFX_ByteString::Concat(const FX_CHAR* pSrcData, FX_STRSIZE nSrcLen) { |
329 if (!pSrcData || nSrcLen <= 0) | 355 if (!pSrcData || nSrcLen <= 0) |
330 return; | 356 return; |
331 | 357 |
332 if (!m_pData) { | 358 if (!m_pData) { |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 return -1; | 691 return -1; |
666 | 692 |
667 FX_STRSIZE nLength = m_pData->m_nDataLength; | 693 FX_STRSIZE nLength = m_pData->m_nDataLength; |
668 while (nLength--) { | 694 while (nLength--) { |
669 if (m_pData->m_String[nLength] == ch) | 695 if (m_pData->m_String[nLength] == ch) |
670 return nLength; | 696 return nLength; |
671 } | 697 } |
672 return -1; | 698 return -1; |
673 } | 699 } |
674 | 700 |
675 const FX_CHAR* FX_strstr(const FX_CHAR* str1, | |
676 int len1, | |
677 const FX_CHAR* str2, | |
678 int len2) { | |
679 if (len2 > len1 || len2 == 0) { | |
680 return nullptr; | |
681 } | |
682 const FX_CHAR* end_ptr = str1 + len1 - len2; | |
683 while (str1 <= end_ptr) { | |
684 int i = 0; | |
685 while (1) { | |
686 if (str1[i] != str2[i]) { | |
687 break; | |
688 } | |
689 i++; | |
690 if (i == len2) { | |
691 return str1; | |
692 } | |
693 } | |
694 str1++; | |
695 } | |
696 return nullptr; | |
697 } | |
698 | |
699 FX_STRSIZE CFX_ByteString::Find(const CFX_ByteStringC& pSub, | 701 FX_STRSIZE CFX_ByteString::Find(const CFX_ByteStringC& pSub, |
700 FX_STRSIZE nStart) const { | 702 FX_STRSIZE nStart) const { |
701 if (!m_pData) | 703 if (!m_pData) |
702 return -1; | 704 return -1; |
703 | 705 |
704 FX_STRSIZE nLength = m_pData->m_nDataLength; | 706 FX_STRSIZE nLength = m_pData->m_nDataLength; |
705 if (nStart > nLength) | 707 if (nStart > nLength) |
706 return -1; | 708 return -1; |
707 | 709 |
708 const FX_CHAR* pStr = | 710 const FX_CHAR* pStr = |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
841 } | 843 } |
842 } | 844 } |
843 if (this_len < that_len) { | 845 if (this_len < that_len) { |
844 return -1; | 846 return -1; |
845 } | 847 } |
846 if (this_len > that_len) { | 848 if (this_len > that_len) { |
847 return 1; | 849 return 1; |
848 } | 850 } |
849 return 0; | 851 return 0; |
850 } | 852 } |
| 853 |
851 void CFX_ByteString::TrimRight(const CFX_ByteStringC& pTargets) { | 854 void CFX_ByteString::TrimRight(const CFX_ByteStringC& pTargets) { |
852 if (!m_pData || pTargets.IsEmpty()) { | 855 if (!m_pData || pTargets.IsEmpty()) { |
853 return; | 856 return; |
854 } | 857 } |
855 ReallocBeforeWrite(m_pData->m_nDataLength); | 858 ReallocBeforeWrite(m_pData->m_nDataLength); |
856 FX_STRSIZE pos = GetLength(); | 859 FX_STRSIZE pos = GetLength(); |
857 if (pos < 1) { | 860 if (pos < 1) { |
858 return; | 861 return; |
859 } | 862 } |
860 while (pos) { | 863 while (pos) { |
861 FX_STRSIZE i = 0; | 864 FX_STRSIZE i = 0; |
862 while (i < pTargets.GetLength() && | 865 while (i < pTargets.GetLength() && |
863 pTargets[i] != m_pData->m_String[pos - 1]) { | 866 pTargets[i] != m_pData->m_String[pos - 1]) { |
864 i++; | 867 i++; |
865 } | 868 } |
866 if (i == pTargets.GetLength()) { | 869 if (i == pTargets.GetLength()) { |
867 break; | 870 break; |
868 } | 871 } |
869 pos--; | 872 pos--; |
870 } | 873 } |
871 if (pos < m_pData->m_nDataLength) { | 874 if (pos < m_pData->m_nDataLength) { |
872 m_pData->m_String[pos] = 0; | 875 m_pData->m_String[pos] = 0; |
873 m_pData->m_nDataLength = pos; | 876 m_pData->m_nDataLength = pos; |
874 } | 877 } |
875 } | 878 } |
| 879 |
876 void CFX_ByteString::TrimRight(FX_CHAR chTarget) { | 880 void CFX_ByteString::TrimRight(FX_CHAR chTarget) { |
877 TrimRight(CFX_ByteStringC(chTarget)); | 881 TrimRight(CFX_ByteStringC(chTarget)); |
878 } | 882 } |
| 883 |
879 void CFX_ByteString::TrimRight() { | 884 void CFX_ByteString::TrimRight() { |
880 TrimRight("\x09\x0a\x0b\x0c\x0d\x20"); | 885 TrimRight("\x09\x0a\x0b\x0c\x0d\x20"); |
881 } | 886 } |
| 887 |
882 void CFX_ByteString::TrimLeft(const CFX_ByteStringC& pTargets) { | 888 void CFX_ByteString::TrimLeft(const CFX_ByteStringC& pTargets) { |
883 if (!m_pData || pTargets.IsEmpty()) | 889 if (!m_pData || pTargets.IsEmpty()) |
884 return; | 890 return; |
885 | 891 |
886 FX_STRSIZE len = GetLength(); | 892 FX_STRSIZE len = GetLength(); |
887 if (len < 1) | 893 if (len < 1) |
888 return; | 894 return; |
889 | 895 |
890 ReallocBeforeWrite(len); | 896 ReallocBeforeWrite(len); |
891 FX_STRSIZE pos = 0; | 897 FX_STRSIZE pos = 0; |
892 while (pos < len) { | 898 while (pos < len) { |
893 FX_STRSIZE i = 0; | 899 FX_STRSIZE i = 0; |
894 while (i < pTargets.GetLength() && pTargets[i] != m_pData->m_String[pos]) { | 900 while (i < pTargets.GetLength() && pTargets[i] != m_pData->m_String[pos]) { |
895 i++; | 901 i++; |
896 } | 902 } |
897 if (i == pTargets.GetLength()) { | 903 if (i == pTargets.GetLength()) { |
898 break; | 904 break; |
899 } | 905 } |
900 pos++; | 906 pos++; |
901 } | 907 } |
902 if (pos) { | 908 if (pos) { |
903 FX_STRSIZE nDataLength = len - pos; | 909 FX_STRSIZE nDataLength = len - pos; |
904 FXSYS_memmove(m_pData->m_String, m_pData->m_String + pos, | 910 FXSYS_memmove(m_pData->m_String, m_pData->m_String + pos, |
905 (nDataLength + 1) * sizeof(FX_CHAR)); | 911 (nDataLength + 1) * sizeof(FX_CHAR)); |
906 m_pData->m_nDataLength = nDataLength; | 912 m_pData->m_nDataLength = nDataLength; |
907 } | 913 } |
908 } | 914 } |
| 915 |
909 void CFX_ByteString::TrimLeft(FX_CHAR chTarget) { | 916 void CFX_ByteString::TrimLeft(FX_CHAR chTarget) { |
910 TrimLeft(CFX_ByteStringC(chTarget)); | 917 TrimLeft(CFX_ByteStringC(chTarget)); |
911 } | 918 } |
| 919 |
912 void CFX_ByteString::TrimLeft() { | 920 void CFX_ByteString::TrimLeft() { |
913 TrimLeft("\x09\x0a\x0b\x0c\x0d\x20"); | 921 TrimLeft("\x09\x0a\x0b\x0c\x0d\x20"); |
914 } | 922 } |
| 923 |
915 uint32_t CFX_ByteString::GetID(FX_STRSIZE start_pos) const { | 924 uint32_t CFX_ByteString::GetID(FX_STRSIZE start_pos) const { |
916 return CFX_ByteStringC(*this).GetID(start_pos); | 925 return CFX_ByteStringC(*this).GetID(start_pos); |
917 } | 926 } |
918 uint32_t CFX_ByteStringC::GetID(FX_STRSIZE start_pos) const { | 927 uint32_t CFX_ByteStringC::GetID(FX_STRSIZE start_pos) const { |
919 if (m_Length == 0) { | 928 if (m_Length == 0) { |
920 return 0; | 929 return 0; |
921 } | 930 } |
922 if (start_pos < 0 || start_pos >= m_Length) { | 931 if (start_pos < 0 || start_pos >= m_Length) { |
923 return 0; | 932 return 0; |
924 } | 933 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
979 fraction %= scale; | 988 fraction %= scale; |
980 scale /= 10; | 989 scale /= 10; |
981 } | 990 } |
982 return buf_size; | 991 return buf_size; |
983 } | 992 } |
984 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) { | 993 CFX_ByteString CFX_ByteString::FormatFloat(FX_FLOAT d, int precision) { |
985 FX_CHAR buf[32]; | 994 FX_CHAR buf[32]; |
986 FX_STRSIZE len = FX_ftoa(d, buf); | 995 FX_STRSIZE len = FX_ftoa(d, buf); |
987 return CFX_ByteString(buf, len); | 996 return CFX_ByteString(buf, len); |
988 } | 997 } |
OLD | NEW |