GNU Radio's DVBS2RX Package
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
27typedef struct {
28 const char* ptr;
29 size_t size;
31
32#ifdef __cplusplus
33static 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.
40static inline StringView view(const char* str, const size_t size) {
42 view.ptr = str;
43 view.size = size;
44 return view;
45}
46
47static 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.
95 size_t dst_size);
96
97// Checks if line contains the specified whitespace separated word.
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.
105 StringView* key,
106 StringView* value);
107
109
110#endif // CPU_FEATURES_INCLUDE_INTERNAL_STRING_VIEW_H_
#define CPU_FEATURES_START_CPP_NAMESPACE
Definition cpu_features_macros.h:127
#define CPU_FEATURES_END_CPP_NAMESPACE
Definition cpu_features_macros.h:128
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)
char CpuFeatures_StringView_Back(const StringView view)
bool CpuFeatures_StringView_HasWord(const StringView line, const char *const word, const char separator)
void CpuFeatures_StringView_CopyString(const StringView src, char *dst, size_t dst_size)
static StringView str(const char *str)
Definition string_view.h:47
bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line, StringView *key, StringView *value)
StringView CpuFeatures_StringView_KeepFront(const StringView str_view, size_t count)
int CpuFeatures_StringView_ParsePositiveNumber(const StringView view)
StringView CpuFeatures_StringView_PopBack(const StringView str_view, size_t count)
StringView CpuFeatures_StringView_PopFront(const StringView str_view, size_t count)
char CpuFeatures_StringView_Front(const StringView view)
int CpuFeatures_StringView_IndexOfChar(const StringView view, char c)
int CpuFeatures_StringView_IndexOf(const StringView view, const StringView sub_view)
StringView CpuFeatures_StringView_TrimWhitespace(StringView view)
bool CpuFeatures_StringView_IsEquals(const StringView a, const StringView b)
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