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 #include "internal/string_view.h"
21 int CpuFeatures_StringView_IndexOfChar(const StringView view, char c) {
22 if (view.ptr && view.size) {
23 const char* const found = (const char*)memchr(view.ptr, c, view.size);
25 return found - view.ptr;
31 int CpuFeatures_StringView_IndexOf(const StringView view,
32 const StringView sub_view) {
34 StringView remainder = view;
35 while (remainder.size >= sub_view.size) {
36 const int found_index =
37 CpuFeatures_StringView_IndexOfChar(remainder, sub_view.ptr[0]);
38 if (found_index < 0) break;
39 remainder = CpuFeatures_StringView_PopFront(remainder, found_index);
40 if (CpuFeatures_StringView_StartsWith(remainder, sub_view)) {
41 return remainder.ptr - view.ptr;
43 remainder = CpuFeatures_StringView_PopFront(remainder, 1);
49 bool CpuFeatures_StringView_IsEquals(const StringView a, const StringView b) {
50 if (a.size == b.size) {
51 return a.ptr == b.ptr || memcmp(a.ptr, b.ptr, b.size) == 0;
56 bool CpuFeatures_StringView_StartsWith(const StringView a, const StringView b) {
57 return a.ptr && b.ptr && b.size && a.size >= b.size
58 ? memcmp(a.ptr, b.ptr, b.size) == 0
62 StringView CpuFeatures_StringView_PopFront(const StringView str_view,
64 if (count > str_view.size) {
65 return kEmptyStringView;
67 return view(str_view.ptr + count, str_view.size - count);
70 StringView CpuFeatures_StringView_PopBack(const StringView str_view,
72 if (count > str_view.size) {
73 return kEmptyStringView;
75 return view(str_view.ptr, str_view.size - count);
78 StringView CpuFeatures_StringView_KeepFront(const StringView str_view,
80 return count <= str_view.size ? view(str_view.ptr, count) : str_view;
83 char CpuFeatures_StringView_Front(const StringView view) {
89 char CpuFeatures_StringView_Back(const StringView view) {
91 return view.ptr[view.size - 1];
94 StringView CpuFeatures_StringView_TrimWhitespace(StringView view) {
95 while (view.size && isspace(CpuFeatures_StringView_Front(view)))
96 view = CpuFeatures_StringView_PopFront(view, 1);
97 while (view.size && isspace(CpuFeatures_StringView_Back(view)))
98 view = CpuFeatures_StringView_PopBack(view, 1);
102 static int HexValue(const char c) {
103 if (c >= '0' && c <= '9') return c - '0';
104 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
105 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
109 // Returns -1 if view contains non digits.
110 static int ParsePositiveNumberWithBase(const StringView view, int base) {
112 StringView remainder = view;
113 for (; remainder.size;
114 remainder = CpuFeatures_StringView_PopFront(remainder, 1)) {
115 const int value = HexValue(CpuFeatures_StringView_Front(remainder));
116 if (value < 0 || value >= base) return -1;
117 result = (result * base) + value;
122 int CpuFeatures_StringView_ParsePositiveNumber(const StringView view) {
124 const StringView hex_prefix = str("0x");
125 if (CpuFeatures_StringView_StartsWith(view, hex_prefix)) {
126 const StringView span_no_prefix =
127 CpuFeatures_StringView_PopFront(view, hex_prefix.size);
128 return ParsePositiveNumberWithBase(span_no_prefix, 16);
130 return ParsePositiveNumberWithBase(view, 10);
135 void CpuFeatures_StringView_CopyString(const StringView src, char* dst,
138 const size_t max_copy_size = dst_size - 1;
139 const size_t copy_size =
140 src.size > max_copy_size ? max_copy_size : src.size;
141 memcpy(dst, src.ptr, copy_size);
142 dst[copy_size] = '\0';
146 bool CpuFeatures_StringView_HasWord(const StringView line,
147 const char* const word_str) {
148 const StringView word = str(word_str);
149 StringView remainder = line;
151 const int index_of_word = CpuFeatures_StringView_IndexOf(remainder, word);
152 if (index_of_word < 0) {
155 const StringView before =
156 CpuFeatures_StringView_KeepFront(line, index_of_word);
157 const StringView after =
158 CpuFeatures_StringView_PopFront(line, index_of_word + word.size);
159 const bool valid_before =
160 before.size == 0 || CpuFeatures_StringView_Back(before) == ' ';
161 const bool valid_after =
162 after.size == 0 || CpuFeatures_StringView_Front(after) == ' ';
163 if (valid_before && valid_after) return true;
165 CpuFeatures_StringView_PopFront(remainder, index_of_word + word.size);
171 bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line,
174 const StringView sep = str(": ");
175 const int index_of_separator = CpuFeatures_StringView_IndexOf(line, sep);
176 if (index_of_separator < 0) return false;
177 *value = CpuFeatures_StringView_TrimWhitespace(
178 CpuFeatures_StringView_PopFront(line, index_of_separator + sep.size));
179 *key = CpuFeatures_StringView_TrimWhitespace(
180 CpuFeatures_StringView_KeepFront(line, index_of_separator));