Vector Optimized Library of Kernels  2.5.0
Architecture-tuned implementations of math kernels
string_view.h
Go to the documentation of this file.
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 
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
36 #endif
37 
38 // Returns a StringView from the provided string.
39 // Passing NULL is valid only if size is 0.
40 static inline StringView view(const char* str, const size_t size) {
42  view.ptr = str;
43  view.size = size;
44  return view;
45 }
46 
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.
51 
52 // Returns the index of the first occurrence of sub_view in view or -1 if not
53 // found.
55  const StringView sub_view);
56 
57 // Returns whether a is equal to b (same content).
59 
60 // Returns whether a starts with b.
62 
63 // Removes count characters from the beginning of view or kEmptyStringView if
64 // count if greater than view.size.
66  size_t count);
67 
68 // Removes count characters from the end of view or kEmptyStringView if count if
69 // greater than view.size.
71  size_t count);
72 
73 // Keeps the count first characters of view or view if count if greater than
74 // view.size.
76  size_t count);
77 
78 // Retrieves the first character of view. If view is empty the behavior is
79 // undefined.
81 
82 // Retrieves the last character of view. If view is empty the behavior is
83 // undefined.
85 
86 // Removes leading and tailing space characters.
88 
89 // Convert StringView to positive integer. e.g. "42", "0x2a".
90 // Returns -1 on error.
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.
99  const char* const word);
100 
101 // Get key/value from line. key and value are separated by ": ".
102 // key and value are cleaned up from leading and trailing whitespaces.
104  StringView* key,
105  StringView* value);
106 
108 
109 #endif // CPU_FEATURES_INCLUDE_INTERNAL_STRING_VIEW_H_
#define CPU_FEATURES_START_CPP_NAMESPACE
Definition: cpu_features_macros.h:114
#define CPU_FEATURES_END_CPP_NAMESPACE
Definition: cpu_features_macros.h:115
static StringView view(const char *str, const size_t size)
Definition: string_view.h:40
bool CpuFeatures_StringView_StartsWith(const StringView a, const StringView b)
Definition: string_view.c:56
char CpuFeatures_StringView_Back(const StringView view)
Definition: string_view.c:89
void CpuFeatures_StringView_CopyString(const StringView src, char *dst, size_t dst_size)
Definition: string_view.c:135
static StringView str(const char *str)
Definition: string_view.h:47
bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line, StringView *key, StringView *value)
Definition: string_view.c:171
StringView CpuFeatures_StringView_KeepFront(const StringView str_view, size_t count)
Definition: string_view.c:78
int CpuFeatures_StringView_ParsePositiveNumber(const StringView view)
Definition: string_view.c:122
StringView CpuFeatures_StringView_PopBack(const StringView str_view, size_t count)
Definition: string_view.c:70
StringView CpuFeatures_StringView_PopFront(const StringView str_view, size_t count)
Definition: string_view.c:62
char CpuFeatures_StringView_Front(const StringView view)
Definition: string_view.c:83
int CpuFeatures_StringView_IndexOfChar(const StringView view, char c)
Definition: string_view.c:21
int CpuFeatures_StringView_IndexOf(const StringView view, const StringView sub_view)
Definition: string_view.c:31
bool CpuFeatures_StringView_HasWord(const StringView line, const char *const word)
Definition: string_view.c:146
StringView CpuFeatures_StringView_TrimWhitespace(StringView view)
Definition: string_view.c:94
bool CpuFeatures_StringView_IsEquals(const StringView a, const StringView b)
Definition: string_view.c:49
static const StringView kEmptyStringView
Definition: string_view.h:35
Definition: string_view.h:27
size_t size
Definition: string_view.h:29
const char * ptr
Definition: string_view.h:28