• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 Google LLC
2 //
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
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14 
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_
18 
19 #include <stdbool.h>
20 #include <stddef.h>
21 #include <string.h>
22 
23 #include "cpu_features_macros.h"
24 
25 CPU_FEATURES_START_CPP_NAMESPACE
26 
27 typedef struct {
28   const char* ptr;
29   size_t size;
30 } StringView;
31 
32 #ifdef __cplusplus
33 static const StringView kEmptyStringView = {NULL, 0};
34 #else
35 static const StringView kEmptyStringView;
36 #endif
37 
38 // Returns a StringView from the provided string.
39 // Passing NULL is valid only if size is 0.
view(const char * str,const size_t size)40 static inline StringView view(const char* str, const size_t size) {
41   StringView view;
42   view.ptr = str;
43   view.size = size;
44   return view;
45 }
46 
str(const char * str)47 static inline StringView str(const char* str) { return view(str, strlen(str)); }
48 
49 // Returns the index of the first occurrence of c in view or -1 if not found.
50 int CpuFeatures_StringView_IndexOfChar(const StringView view, char c);
51 
52 // Returns the index of the first occurrence of sub_view in view or -1 if not
53 // found.
54 int CpuFeatures_StringView_IndexOf(const StringView view,
55                                    const StringView sub_view);
56 
57 // Returns whether a is equal to b (same content).
58 bool CpuFeatures_StringView_IsEquals(const StringView a, const StringView b);
59 
60 // Returns whether a starts with b.
61 bool CpuFeatures_StringView_StartsWith(const StringView a, const StringView b);
62 
63 // Removes count characters from the beginning of view or kEmptyStringView if
64 // count if greater than view.size.
65 StringView CpuFeatures_StringView_PopFront(const StringView str_view,
66                                            size_t count);
67 
68 // Removes count characters from the end of view or kEmptyStringView if count if
69 // greater than view.size.
70 StringView CpuFeatures_StringView_PopBack(const StringView str_view,
71                                           size_t count);
72 
73 // Keeps the count first characters of view or view if count if greater than
74 // view.size.
75 StringView CpuFeatures_StringView_KeepFront(const StringView str_view,
76                                             size_t count);
77 
78 // Retrieves the first character of view. If view is empty the behavior is
79 // undefined.
80 char CpuFeatures_StringView_Front(const StringView view);
81 
82 // Retrieves the last character of view. If view is empty the behavior is
83 // undefined.
84 char CpuFeatures_StringView_Back(const StringView view);
85 
86 // Removes leading and tailing space characters.
87 StringView CpuFeatures_StringView_TrimWhitespace(StringView view);
88 
89 // Convert StringView to positive integer. e.g. "42", "0x2a".
90 // Returns -1 on error.
91 int CpuFeatures_StringView_ParsePositiveNumber(const StringView view);
92 
93 // Copies src StringView to dst buffer.
94 void CpuFeatures_StringView_CopyString(const StringView src, char* dst,
95                                        size_t dst_size);
96 
97 // Checks if line contains the specified whitespace separated word.
98 bool CpuFeatures_StringView_HasWord(const StringView line,
99                                     const char* const word,
100                                     const char separator);
101 
102 // Get key/value from line. key and value are separated by ": ".
103 // key and value are cleaned up from leading and trailing whitespaces.
104 bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line,
105                                                  StringView* key,
106                                                  StringView* value);
107 
108 CPU_FEATURES_END_CPP_NAMESPACE
109 
110 #endif  // CPU_FEATURES_INCLUDE_INTERNAL_STRING_VIEW_H_
111