1 // Copyright 2017 Google Inc.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // A view over a piece of string. The view is not 0 terminated.
16 #ifndef CPU_FEATURES_INCLUDE_INTERNAL_STRING_VIEW_H_
17 #define CPU_FEATURES_INCLUDE_INTERNAL_STRING_VIEW_H_
22 #include "cpu_features_macros.h"
24 CPU_FEATURES_START_CPP_NAMESPACE
32 static const StringView kEmptyStringView = {NULL, 0};
34 static const StringView kEmptyStringView;
37 // Returns a StringView from the provided string.
38 // Passing NULL is valid only if size is 0.
39 static inline StringView view(const char* str, const size_t size) {
46 static inline StringView str(const char* str) { return view(str, strlen(str)); }
48 // Returns the index of the first occurrence of c in view or -1 if not found.
49 int CpuFeatures_StringView_IndexOfChar(const StringView view, char c);
51 // Returns the index of the first occurrence of sub_view in view or -1 if not
53 int CpuFeatures_StringView_IndexOf(const StringView view,
54 const StringView sub_view);
56 // Returns whether a is equal to b (same content).
57 bool CpuFeatures_StringView_IsEquals(const StringView a, const StringView b);
59 // Returns whether a starts with b.
60 bool CpuFeatures_StringView_StartsWith(const StringView a, const StringView b);
62 // Removes count characters from the beginning of view or kEmptyStringView if
63 // count if greater than view.size.
64 StringView CpuFeatures_StringView_PopFront(const StringView str_view,
67 // Removes count characters from the end of view or kEmptyStringView if count if
68 // greater than view.size.
69 StringView CpuFeatures_StringView_PopBack(const StringView str_view,
72 // Keeps the count first characters of view or view if count if greater than
74 StringView CpuFeatures_StringView_KeepFront(const StringView str_view,
77 // Retrieves the first character of view. If view is empty the behavior is
79 char CpuFeatures_StringView_Front(const StringView view);
81 // Retrieves the last character of view. If view is empty the behavior is
83 char CpuFeatures_StringView_Back(const StringView view);
85 // Removes leading and tailing space characters.
86 StringView CpuFeatures_StringView_TrimWhitespace(StringView view);
88 // Convert StringView to positive integer. e.g. "42", "0x2a".
89 // Returns -1 on error.
90 int CpuFeatures_StringView_ParsePositiveNumber(const StringView view);
92 // Copies src StringView to dst buffer.
93 void CpuFeatures_StringView_CopyString(const StringView src, char* dst,
96 // Checks if line contains the specified whitespace separated word.
97 bool CpuFeatures_StringView_HasWord(const StringView line,
98 const char* const word);
100 // Get key/value from line. key and value are separated by ": ".
101 // key and value are cleaned up from leading and trailing whitespaces.
102 bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line,
106 CPU_FEATURES_END_CPP_NAMESPACE
108 #endif // CPU_FEATURES_INCLUDE_INTERNAL_STRING_VIEW_H_