xref: /aosp_15_r20/external/cpu_features/include/internal/string_view.h (revision eca53ba6d2e951e174b64682eaf56a36b8204c89)
1*eca53ba6SRoland Levillain // Copyright 2017 Google LLC
2*eca53ba6SRoland Levillain //
3*eca53ba6SRoland Levillain // Licensed under the Apache License, Version 2.0 (the "License");
4*eca53ba6SRoland Levillain // you may not use this file except in compliance with the License.
5*eca53ba6SRoland Levillain // You may obtain a copy of the License at
6*eca53ba6SRoland Levillain //
7*eca53ba6SRoland Levillain //    http://www.apache.org/licenses/LICENSE-2.0
8*eca53ba6SRoland Levillain //
9*eca53ba6SRoland Levillain // Unless required by applicable law or agreed to in writing, software
10*eca53ba6SRoland Levillain // distributed under the License is distributed on an "AS IS" BASIS,
11*eca53ba6SRoland Levillain // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*eca53ba6SRoland Levillain // See the License for the specific language governing permissions and
13*eca53ba6SRoland Levillain // limitations under the License.
14*eca53ba6SRoland Levillain 
15*eca53ba6SRoland Levillain // A view over a piece of string. The view is not 0 terminated.
16*eca53ba6SRoland Levillain #ifndef CPU_FEATURES_INCLUDE_INTERNAL_STRING_VIEW_H_
17*eca53ba6SRoland Levillain #define CPU_FEATURES_INCLUDE_INTERNAL_STRING_VIEW_H_
18*eca53ba6SRoland Levillain 
19*eca53ba6SRoland Levillain #include <stdbool.h>
20*eca53ba6SRoland Levillain #include <stddef.h>
21*eca53ba6SRoland Levillain #include <string.h>
22*eca53ba6SRoland Levillain 
23*eca53ba6SRoland Levillain #include "cpu_features_macros.h"
24*eca53ba6SRoland Levillain 
25*eca53ba6SRoland Levillain CPU_FEATURES_START_CPP_NAMESPACE
26*eca53ba6SRoland Levillain 
27*eca53ba6SRoland Levillain typedef struct {
28*eca53ba6SRoland Levillain   const char* ptr;
29*eca53ba6SRoland Levillain   size_t size;
30*eca53ba6SRoland Levillain } StringView;
31*eca53ba6SRoland Levillain 
32*eca53ba6SRoland Levillain #ifdef __cplusplus
33*eca53ba6SRoland Levillain static const StringView kEmptyStringView = {NULL, 0};
34*eca53ba6SRoland Levillain #else
35*eca53ba6SRoland Levillain static const StringView kEmptyStringView;
36*eca53ba6SRoland Levillain #endif
37*eca53ba6SRoland Levillain 
38*eca53ba6SRoland Levillain // Returns a StringView from the provided string.
39*eca53ba6SRoland Levillain // Passing NULL is valid only if size is 0.
view(const char * str,const size_t size)40*eca53ba6SRoland Levillain static inline StringView view(const char* str, const size_t size) {
41*eca53ba6SRoland Levillain   StringView view;
42*eca53ba6SRoland Levillain   view.ptr = str;
43*eca53ba6SRoland Levillain   view.size = size;
44*eca53ba6SRoland Levillain   return view;
45*eca53ba6SRoland Levillain }
46*eca53ba6SRoland Levillain 
str(const char * str)47*eca53ba6SRoland Levillain static inline StringView str(const char* str) { return view(str, strlen(str)); }
48*eca53ba6SRoland Levillain 
49*eca53ba6SRoland Levillain // Returns the index of the first occurrence of c in view or -1 if not found.
50*eca53ba6SRoland Levillain int CpuFeatures_StringView_IndexOfChar(const StringView view, char c);
51*eca53ba6SRoland Levillain 
52*eca53ba6SRoland Levillain // Returns the index of the first occurrence of sub_view in view or -1 if not
53*eca53ba6SRoland Levillain // found.
54*eca53ba6SRoland Levillain int CpuFeatures_StringView_IndexOf(const StringView view,
55*eca53ba6SRoland Levillain                                    const StringView sub_view);
56*eca53ba6SRoland Levillain 
57*eca53ba6SRoland Levillain // Returns whether a is equal to b (same content).
58*eca53ba6SRoland Levillain bool CpuFeatures_StringView_IsEquals(const StringView a, const StringView b);
59*eca53ba6SRoland Levillain 
60*eca53ba6SRoland Levillain // Returns whether a starts with b.
61*eca53ba6SRoland Levillain bool CpuFeatures_StringView_StartsWith(const StringView a, const StringView b);
62*eca53ba6SRoland Levillain 
63*eca53ba6SRoland Levillain // Removes count characters from the beginning of view or kEmptyStringView if
64*eca53ba6SRoland Levillain // count if greater than view.size.
65*eca53ba6SRoland Levillain StringView CpuFeatures_StringView_PopFront(const StringView str_view,
66*eca53ba6SRoland Levillain                                            size_t count);
67*eca53ba6SRoland Levillain 
68*eca53ba6SRoland Levillain // Removes count characters from the end of view or kEmptyStringView if count if
69*eca53ba6SRoland Levillain // greater than view.size.
70*eca53ba6SRoland Levillain StringView CpuFeatures_StringView_PopBack(const StringView str_view,
71*eca53ba6SRoland Levillain                                           size_t count);
72*eca53ba6SRoland Levillain 
73*eca53ba6SRoland Levillain // Keeps the count first characters of view or view if count if greater than
74*eca53ba6SRoland Levillain // view.size.
75*eca53ba6SRoland Levillain StringView CpuFeatures_StringView_KeepFront(const StringView str_view,
76*eca53ba6SRoland Levillain                                             size_t count);
77*eca53ba6SRoland Levillain 
78*eca53ba6SRoland Levillain // Retrieves the first character of view. If view is empty the behavior is
79*eca53ba6SRoland Levillain // undefined.
80*eca53ba6SRoland Levillain char CpuFeatures_StringView_Front(const StringView view);
81*eca53ba6SRoland Levillain 
82*eca53ba6SRoland Levillain // Retrieves the last character of view. If view is empty the behavior is
83*eca53ba6SRoland Levillain // undefined.
84*eca53ba6SRoland Levillain char CpuFeatures_StringView_Back(const StringView view);
85*eca53ba6SRoland Levillain 
86*eca53ba6SRoland Levillain // Removes leading and tailing space characters.
87*eca53ba6SRoland Levillain StringView CpuFeatures_StringView_TrimWhitespace(StringView view);
88*eca53ba6SRoland Levillain 
89*eca53ba6SRoland Levillain // Convert StringView to positive integer. e.g. "42", "0x2a".
90*eca53ba6SRoland Levillain // Returns -1 on error.
91*eca53ba6SRoland Levillain int CpuFeatures_StringView_ParsePositiveNumber(const StringView view);
92*eca53ba6SRoland Levillain 
93*eca53ba6SRoland Levillain // Copies src StringView to dst buffer.
94*eca53ba6SRoland Levillain void CpuFeatures_StringView_CopyString(const StringView src, char* dst,
95*eca53ba6SRoland Levillain                                        size_t dst_size);
96*eca53ba6SRoland Levillain 
97*eca53ba6SRoland Levillain // Checks if line contains the specified whitespace separated word.
98*eca53ba6SRoland Levillain bool CpuFeatures_StringView_HasWord(const StringView line,
99*eca53ba6SRoland Levillain                                     const char* const word,
100*eca53ba6SRoland Levillain                                     const char separator);
101*eca53ba6SRoland Levillain 
102*eca53ba6SRoland Levillain // Get key/value from line. key and value are separated by ": ".
103*eca53ba6SRoland Levillain // key and value are cleaned up from leading and trailing whitespaces.
104*eca53ba6SRoland Levillain bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line,
105*eca53ba6SRoland Levillain                                                  StringView* key,
106*eca53ba6SRoland Levillain                                                  StringView* value);
107*eca53ba6SRoland Levillain 
108*eca53ba6SRoland Levillain CPU_FEATURES_END_CPP_NAMESPACE
109*eca53ba6SRoland Levillain 
110*eca53ba6SRoland Levillain #endif  // CPU_FEATURES_INCLUDE_INTERNAL_STRING_VIEW_H_
111