Guillaume Chatelet | 3cc8f31 | 2020-10-12 08:55:20 +0000 | [diff] [blame] | 1 | // Copyright 2017 Google LLC |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 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 | #include "internal/string_view.h" |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <ctype.h> |
| 19 | #include <string.h> |
| 20 | |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 21 | int CpuFeatures_StringView_IndexOfChar(const StringView view, char c) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 22 | if (view.ptr && view.size) { |
| 23 | const char* const found = (const char*)memchr(view.ptr, c, view.size); |
| 24 | if (found) { |
Leonard Mosescu | bdb36d9 | 2019-07-03 05:57:19 -0700 | [diff] [blame] | 25 | return (int)(found - view.ptr); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | return -1; |
| 29 | } |
| 30 | |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 31 | int CpuFeatures_StringView_IndexOf(const StringView view, |
| 32 | const StringView sub_view) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 33 | if (sub_view.size) { |
| 34 | StringView remainder = view; |
| 35 | while (remainder.size >= sub_view.size) { |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 36 | const int found_index = |
| 37 | CpuFeatures_StringView_IndexOfChar(remainder, sub_view.ptr[0]); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 38 | if (found_index < 0) break; |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 39 | remainder = CpuFeatures_StringView_PopFront(remainder, found_index); |
| 40 | if (CpuFeatures_StringView_StartsWith(remainder, sub_view)) { |
Leonard Mosescu | bdb36d9 | 2019-07-03 05:57:19 -0700 | [diff] [blame] | 41 | return (int)(remainder.ptr - view.ptr); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 42 | } |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 43 | remainder = CpuFeatures_StringView_PopFront(remainder, 1); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | return -1; |
| 47 | } |
| 48 | |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 49 | bool CpuFeatures_StringView_IsEquals(const StringView a, const StringView b) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 50 | if (a.size == b.size) { |
| 51 | return a.ptr == b.ptr || memcmp(a.ptr, b.ptr, b.size) == 0; |
| 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 56 | bool CpuFeatures_StringView_StartsWith(const StringView a, const StringView b) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 57 | return a.ptr && b.ptr && b.size && a.size >= b.size |
| 58 | ? memcmp(a.ptr, b.ptr, b.size) == 0 |
| 59 | : false; |
| 60 | } |
| 61 | |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 62 | StringView CpuFeatures_StringView_PopFront(const StringView str_view, |
| 63 | size_t count) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 64 | if (count > str_view.size) { |
| 65 | return kEmptyStringView; |
| 66 | } |
| 67 | return view(str_view.ptr + count, str_view.size - count); |
| 68 | } |
| 69 | |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 70 | StringView CpuFeatures_StringView_PopBack(const StringView str_view, |
| 71 | size_t count) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 72 | if (count > str_view.size) { |
| 73 | return kEmptyStringView; |
| 74 | } |
| 75 | return view(str_view.ptr, str_view.size - count); |
| 76 | } |
| 77 | |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 78 | StringView CpuFeatures_StringView_KeepFront(const StringView str_view, |
| 79 | size_t count) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 80 | return count <= str_view.size ? view(str_view.ptr, count) : str_view; |
| 81 | } |
| 82 | |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 83 | char CpuFeatures_StringView_Front(const StringView view) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 84 | assert(view.size); |
| 85 | assert(view.ptr); |
| 86 | return view.ptr[0]; |
| 87 | } |
| 88 | |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 89 | char CpuFeatures_StringView_Back(const StringView view) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 90 | assert(view.size); |
| 91 | return view.ptr[view.size - 1]; |
| 92 | } |
| 93 | |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 94 | StringView CpuFeatures_StringView_TrimWhitespace(StringView view) { |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 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); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 99 | return view; |
| 100 | } |
| 101 | |
| 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; |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | // Returns -1 if view contains non digits. |
| 110 | static int ParsePositiveNumberWithBase(const StringView view, int base) { |
| 111 | int result = 0; |
| 112 | StringView remainder = view; |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 113 | for (; remainder.size; |
| 114 | remainder = CpuFeatures_StringView_PopFront(remainder, 1)) { |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 115 | const int value = HexValue(CpuFeatures_StringView_Front(remainder)); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 116 | if (value < 0 || value >= base) return -1; |
| 117 | result = (result * base) + value; |
| 118 | } |
| 119 | return result; |
| 120 | } |
| 121 | |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 122 | int CpuFeatures_StringView_ParsePositiveNumber(const StringView view) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 123 | if (view.size) { |
| 124 | const StringView hex_prefix = str("0x"); |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 125 | if (CpuFeatures_StringView_StartsWith(view, hex_prefix)) { |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 126 | const StringView span_no_prefix = |
| 127 | CpuFeatures_StringView_PopFront(view, hex_prefix.size); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 128 | return ParsePositiveNumberWithBase(span_no_prefix, 16); |
| 129 | } |
| 130 | return ParsePositiveNumberWithBase(view, 10); |
| 131 | } |
| 132 | return -1; |
| 133 | } |
| 134 | |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 135 | void CpuFeatures_StringView_CopyString(const StringView src, char* dst, |
| 136 | size_t dst_size) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 137 | if (dst_size > 0) { |
| 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'; |
| 143 | } |
| 144 | } |
| 145 | |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 146 | bool CpuFeatures_StringView_HasWord(const StringView line, |
| 147 | const char* const word_str) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 148 | const StringView word = str(word_str); |
| 149 | StringView remainder = line; |
| 150 | for (;;) { |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 151 | const int index_of_word = CpuFeatures_StringView_IndexOf(remainder, word); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 152 | if (index_of_word < 0) { |
| 153 | return false; |
| 154 | } else { |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 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) == ' '; |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 163 | if (valid_before && valid_after) return true; |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 164 | remainder = |
| 165 | CpuFeatures_StringView_PopFront(remainder, index_of_word + word.size); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | return false; |
| 169 | } |
| 170 | |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 171 | bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line, |
| 172 | StringView* key, |
| 173 | StringView* value) { |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 174 | const StringView sep = str(": "); |
Arvid Gerstmann | a1ffdcb | 2018-04-26 10:31:03 +0200 | [diff] [blame] | 175 | const int index_of_separator = CpuFeatures_StringView_IndexOf(line, sep); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 176 | if (index_of_separator < 0) return false; |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 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)); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 181 | return true; |
| 182 | } |