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