Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sol-str-slice.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Soletta (TM) Project
3  *
4  * Copyright (C) 2015 Intel Corporation. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <ctype.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <strings.h>
26 
27 #include <sol-macros.h>
28 #include <sol-types.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
57 #define SOL_STR_STATIC_ASSERT_LITERAL(_s) ("" _s)
58 
62 #define SOL_STR_SLICE_LITERAL(_s) { (sizeof(SOL_STR_STATIC_ASSERT_LITERAL(_s)) - 1), (_s) }
63 
67 #define SOL_STR_SLICE_STR(_s, _len) (struct sol_str_slice){.len = (_len), .data = (_s) }
68 
72 #define SOL_STR_SLICE_EMPTY { .len = 0, .data = "" }
73 
78 #define SOL_STR_SLICE_PRINT(_s) (int)(_s).len, (_s).data
79 
84 typedef struct sol_str_slice {
85  size_t len;
86  const char *data;
88 
99 static inline bool
100 sol_str_slice_str_eq(const struct sol_str_slice a, const char *b)
101 {
102  return b && a.len == strlen(b) && (memcmp(a.data, b, a.len) == 0);
103 }
104 
115 static inline bool
116 sol_str_slice_eq(const struct sol_str_slice a, const struct sol_str_slice b)
117 {
118  return a.len == b.len && (memcmp(a.data, b.data, a.len) == 0);
119 }
120 
133 static inline bool
134 sol_str_slice_str_case_eq(const struct sol_str_slice a, const char *b)
135 {
136  return b && a.len == strlen(b) && (strncasecmp(a.data, b, a.len) == 0);
137 }
138 
151 static inline bool
152 sol_str_slice_case_eq(const struct sol_str_slice a, const struct sol_str_slice b)
153 {
154  return a.len == b.len && (strncasecmp(a.data, b.data, a.len) == 0);
155 }
156 
165 char *sol_str_slice_contains(const struct sol_str_slice haystack, const struct sol_str_slice needle);
166 
175 static inline char *
176 sol_str_slice_str_contains(const struct sol_str_slice haystack, const char *needle)
177 {
178  return sol_str_slice_contains(haystack, SOL_STR_SLICE_STR(needle, strlen(needle)));
179 }
180 
189 static inline void
190 sol_str_slice_copy(char *dst, const struct sol_str_slice src)
191 {
192  memcpy(dst, src.data, src.len);
193  dst[src.len] = 0;
194 }
195 
204 static inline bool
205 sol_str_slice_starts_with(const struct sol_str_slice slice, const struct sol_str_slice prefix)
206 {
207  return slice.len >= prefix.len && strncmp(slice.data, prefix.data, prefix.len) == 0;
208 }
209 
218 static inline bool
219 sol_str_slice_str_starts_with(const struct sol_str_slice slice, const char *prefix)
220 {
221  size_t len = strlen(prefix);
222 
223  return slice.len >= len && strncmp(slice.data, prefix, len) == 0;
224 }
225 
233 static
234 #ifndef DOXYGEN_RUN
236 #endif
237 inline struct sol_str_slice
239 {
240  return SOL_STR_SLICE_STR(s, strlen(s));
241 }
242 
250 static
251 #ifndef DOXYGEN_RUN
253 #endif
254 inline struct sol_str_slice
256 {
257  return SOL_STR_SLICE_STR((char *)blob->mem, blob->size);
258 }
259 
269 static inline struct sol_blob *
271 {
272  void *blob_mem;
273  struct sol_blob *blob;
274 
275  blob_mem = malloc(slice.len);
276  if (!blob_mem)
277  return NULL;
278 
279  memcpy(blob_mem, slice.data, slice.len);
280 
281  blob = sol_blob_new(&SOL_BLOB_TYPE_DEFAULT, NULL,
282  blob_mem, slice.len);
283 
284  if (!blob) {
285  free(blob_mem);
286  return NULL;
287  }
288 
289  return blob;
290 }
291 
300 int sol_str_slice_to_int(const struct sol_str_slice s, long int *value);
301 
311 static inline char *
313 {
314  return strndup(slice.data, slice.len);
315 }
316 
324 static inline struct sol_str_slice
326 {
327  struct sol_str_slice copy = slice;
328 
329  while (copy.len && isspace((uint8_t)*copy.data)) {
330  copy.data++;
331  copy.len--;
332  }
333 
334  return copy;
335 }
336 
344 static inline struct sol_str_slice
346 {
347  struct sol_str_slice copy = slice;
348 
349  while (copy.len != 0) {
350  uint8_t c = copy.data[copy.len - 1];
351  if (isspace(c))
352  copy.len--;
353  else
354  break;
355  }
356 
357  if (slice.len - copy.len)
358  return copy;
359 
360  return slice;
361 }
362 
370 static inline struct sol_str_slice
372 {
375 }
376 
394 struct sol_vector sol_str_slice_split(const struct sol_str_slice slice, const char *delim, size_t maxsplit);
395 
418 bool sol_str_slice_split_iterate(const struct sol_str_slice slice, struct sol_str_slice *token, const char **itr, const struct sol_str_slice delim);
419 
430 static inline bool
431 sol_str_slice_str_split_iterate(const struct sol_str_slice slice, struct sol_str_slice *token, const char **itr, const char *delim)
432 {
433  return sol_str_slice_split_iterate(slice, token, itr, sol_str_slice_from_str(delim));
434 }
435 
440 #ifdef __cplusplus
441 }
442 #endif
static bool sol_str_slice_str_case_eq(const struct sol_str_slice a, const char *b)
Checks if the content of the slice is equal to the string.
Definition: sol-str-slice.h:134
bool sol_str_slice_split_iterate(const struct sol_str_slice slice, struct sol_str_slice *token, const char **itr, const struct sol_str_slice delim)
Do an one step split iteration over a slice.
static bool sol_str_slice_starts_with(const struct sol_str_slice slice, const struct sol_str_slice prefix)
Checks if slice begins with prefix.
Definition: sol-str-slice.h:205
static char * sol_str_slice_str_contains(const struct sol_str_slice haystack, const char *needle)
Checks if haystack contains needle.
Definition: sol-str-slice.h:176
struct sol_str_slice sol_str_slice
String slice type.
These routines are used for Soletta types' manipulation.
static struct sol_str_slice sol_str_slice_trim(struct sol_str_slice slice)
Returns a slice based on slice but without either leading or trailing white spaces.
Definition: sol-str-slice.h:371
static struct sol_str_slice sol_str_slice_remove_trailing_whitespace(struct sol_str_slice slice)
Returns a slice based on slice but without trailing white spaces.
Definition: sol-str-slice.h:345
struct sol_blob * sol_blob_new(const struct sol_blob_type *type, struct sol_blob *parent, const void *mem, size_t size)
Creates a new blob instance of the given type type.
These are common Soletta macros.
const struct sol_blob_type SOL_BLOB_TYPE_DEFAULT
Blob type object for the default implementation.
static struct sol_str_slice sol_str_slice_from_blob(const struct sol_blob *blob)
Populates a slice from a sol_blob.
Definition: sol-str-slice.h:255
static struct sol_buffer value
Definition: server.c:42
static bool sol_str_slice_str_starts_with(const struct sol_str_slice slice, const char *prefix)
Checks if slice begins with prefix.
Definition: sol-str-slice.h:219
String slice type.
Definition: sol-str-slice.h:84
static struct sol_str_slice sol_str_slice_remove_leading_whitespace(struct sol_str_slice slice)
Returns a slice based on slice but without leading white spaces.
Definition: sol-str-slice.h:325
static struct sol_str_slice sol_str_slice_from_str(const char *s)
Populates a slice from a string.
Definition: sol-str-slice.h:238
const char * data
Slice data.
Definition: sol-str-slice.h:86
char * sol_str_slice_contains(const struct sol_str_slice haystack, const struct sol_str_slice needle)
Checks if haystack contains needle.
Data type describing the default blob implementation.
Definition: sol-types.h:468
#define SOL_ATTR_NON_NULL(...)
Specifies that some function parameters should be non-null pointers.
Definition: sol-macros.h:193
#define SOL_STR_SLICE_STR(_s, _len)
Helper macro to make easier to declare a string slice from a string.
Definition: sol-str-slice.h:67
static bool sol_str_slice_case_eq(const struct sol_str_slice a, const struct sol_str_slice b)
Checks if the content of both slices are equal.
Definition: sol-str-slice.h:152
int sol_str_slice_to_int(const struct sol_str_slice s, long int *value)
Converts a string slice to an integer.
Soletta vector is an array that grows dynamically.
Definition: sol-vector.h:58
static struct sol_blob * sol_str_slice_to_blob(const struct sol_str_slice slice)
Creates a blob from a slice.
Definition: sol-str-slice.h:270
static char * sol_str_slice_to_str(const struct sol_str_slice slice)
Creates a string from a string slice.
Definition: sol-str-slice.h:312
static bool sol_str_slice_eq(const struct sol_str_slice a, const struct sol_str_slice b)
Checks if the content of both slices are equal.
Definition: sol-str-slice.h:116
static bool sol_str_slice_str_eq(const struct sol_str_slice a, const char *b)
Checks if the content of the slice is equal to the string.
Definition: sol-str-slice.h:100
size_t len
Slice length.
Definition: sol-str-slice.h:85
static bool sol_str_slice_str_split_iterate(const struct sol_str_slice slice, struct sol_str_slice *token, const char **itr, const char *delim)
Wrapper over sol_str_slice_split_iterate()
Definition: sol-str-slice.h:431
static void sol_str_slice_copy(char *dst, const struct sol_str_slice src)
Copies the content of slice src into string dst.
Definition: sol-str-slice.h:190
struct sol_vector sol_str_slice_split(const struct sol_str_slice slice, const char *delim, size_t maxsplit)
Return a list of the words in a given string slice, using delim as delimiter string.