/src/icu/icu4c/source/common/stringpiece.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | // © 2016 and later: Unicode, Inc. and others.  | 
2  |  | // License & terms of use: http://www.unicode.org/copyright.html  | 
3  |  | // Copyright (C) 2009-2013, International Business Machines  | 
4  |  | // Corporation and others. All Rights Reserved.  | 
5  |  | //  | 
6  |  | // Copyright 2004 and onwards Google Inc.  | 
7  |  | //  | 
8  |  | // Author: wilsonh@google.com (Wilson Hsieh)  | 
9  |  | //  | 
10  |  |  | 
11  |  | #include "unicode/utypes.h"  | 
12  |  | #include "unicode/stringpiece.h"  | 
13  |  | #include "cstring.h"  | 
14  |  | #include "cmemory.h"  | 
15  |  |  | 
16  |  | U_NAMESPACE_BEGIN  | 
17  |  |  | 
18  |  | StringPiece::StringPiece(const char* str)  | 
19  | 0  |     : ptr_(str), length_((str == nullptr) ? 0 : static_cast<int32_t>(uprv_strlen(str))) { } | 
20  |  |  | 
21  | 0  | StringPiece::StringPiece(const StringPiece& x, int32_t pos) { | 
22  | 0  |   if (pos < 0) { | 
23  | 0  |     pos = 0;  | 
24  | 0  |   } else if (pos > x.length_) { | 
25  | 0  |     pos = x.length_;  | 
26  | 0  |   }  | 
27  | 0  |   ptr_ = x.ptr_ + pos;  | 
28  | 0  |   length_ = x.length_ - pos;  | 
29  | 0  | }  | 
30  |  |  | 
31  | 0  | StringPiece::StringPiece(const StringPiece& x, int32_t pos, int32_t len) { | 
32  | 0  |   if (pos < 0) { | 
33  | 0  |     pos = 0;  | 
34  | 0  |   } else if (pos > x.length_) { | 
35  | 0  |     pos = x.length_;  | 
36  | 0  |   }  | 
37  | 0  |   if (len < 0) { | 
38  | 0  |     len = 0;  | 
39  | 0  |   } else if (len > x.length_ - pos) { | 
40  | 0  |     len = x.length_ - pos;  | 
41  | 0  |   }  | 
42  | 0  |   ptr_ = x.ptr_ + pos;  | 
43  | 0  |   length_ = len;  | 
44  | 0  | }  | 
45  |  |  | 
46  | 0  | void StringPiece::set(const char* str) { | 
47  | 0  |   ptr_ = str;  | 
48  | 0  |   if (str != nullptr)  | 
49  | 0  |     length_ = static_cast<int32_t>(uprv_strlen(str));  | 
50  | 0  |   else  | 
51  | 0  |     length_ = 0;  | 
52  | 0  | }  | 
53  |  |  | 
54  | 0  | int32_t StringPiece::find(StringPiece needle, int32_t offset) { | 
55  | 0  |   if (length() == 0 && needle.length() == 0) { | 
56  | 0  |     return 0;  | 
57  | 0  |   }  | 
58  |  |   // TODO: Improve to be better than O(N^2)?  | 
59  | 0  |   for (int32_t i = offset; i < length(); i++) { | 
60  | 0  |     int32_t j = 0;  | 
61  | 0  |     for (; j < needle.length(); i++, j++) { | 
62  | 0  |       if (data()[i] != needle.data()[j]) { | 
63  | 0  |         i -= j;  | 
64  | 0  |         goto outer_end;  | 
65  | 0  |       }  | 
66  | 0  |     }  | 
67  | 0  |     return i - j;  | 
68  | 0  |     outer_end: void();  | 
69  | 0  |   }  | 
70  | 0  |   return -1;  | 
71  | 0  | }  | 
72  |  |  | 
73  | 0  | int32_t StringPiece::compare(StringPiece other) { | 
74  | 0  |   int32_t i = 0;  | 
75  | 0  |   for (; i < length(); i++) { | 
76  | 0  |     if (i == other.length()) { | 
77  |  |       // this is longer  | 
78  | 0  |       return 1;  | 
79  | 0  |     }  | 
80  | 0  |     char a = data()[i];  | 
81  | 0  |     char b = other.data()[i];  | 
82  | 0  |     if (a < b) { | 
83  | 0  |       return -1;  | 
84  | 0  |     } else if (a > b) { | 
85  | 0  |       return 1;  | 
86  | 0  |     }  | 
87  | 0  |   }  | 
88  | 0  |   if (i < other.length()) { | 
89  |  |     // other is longer  | 
90  | 0  |     return -1;  | 
91  | 0  |   }  | 
92  | 0  |   return 0;  | 
93  | 0  | }  | 
94  |  |  | 
95  |  | U_EXPORT UBool U_EXPORT2  | 
96  | 0  | operator==(const StringPiece& x, const StringPiece& y) { | 
97  | 0  |   int32_t len = x.size();  | 
98  | 0  |   if (len != y.size()) { | 
99  | 0  |     return false;  | 
100  | 0  |   }  | 
101  | 0  |   if (len == 0) { | 
102  | 0  |     return true;  | 
103  | 0  |   }  | 
104  | 0  |   const char* p = x.data();  | 
105  | 0  |   const char* p2 = y.data();  | 
106  |  |   // Test last byte in case strings share large common prefix  | 
107  | 0  |   --len;  | 
108  | 0  |   if (p[len] != p2[len]) return false;  | 
109  |  |   // At this point we can, but don't have to, ignore the last byte.  | 
110  | 0  |   return uprv_memcmp(p, p2, len) == 0;  | 
111  | 0  | }  | 
112  |  |  | 
113  |  |  | 
114  |  | const int32_t StringPiece::npos = 0x7fffffff;  | 
115  |  |  | 
116  |  | U_NAMESPACE_END  |