string_util.h
Go to the documentation of this file.
1 /* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2 All rights reserved.
3 
4 This software was developed in the APRIL Robotics Lab under the
5 direction of Edwin Olson, ebolson@umich.edu. This software may be
6 available under alternative licensing terms; contact the address above.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 The views and conclusions contained in the software and documentation are those
29 of the authors and should not be interpreted as representing official policies,
30 either expressed or implied, of the Regents of The University of Michigan.
31 */
32 
33 #ifndef _STRING_UTIL_H
34 #define _STRING_UTIL_H
35 
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <stdbool.h>
39 #include <ctype.h>
40 
41 #include "common/zarray.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 
49 
52 {
53  char *s;
54  size_t len;
55  size_t pos;
56 
57  int line, col;
58 };
59 
65 char *sprintf_alloc(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
66 
72 char *vsprintf_alloc(const char *fmt, va_list args);
73 
78 #define str_concat(...) _str_concat_private(__VA_ARGS__, NULL)
79 char *_str_concat_private(const char *first, ...);
80 
81 
82 // Returns the index of the first character that differs:
83 int str_diff_idx(const char * a, const char * b);
84 
99 zarray_t *str_split(const char *str, const char *delim);
100 
101 zarray_t *str_split_spaces(const char *str);
102 
104 
105 /*
106  * Determines if str1 exactly matches str2 (more efficient than strcmp(...) == 0)
107  */
108 static inline bool streq(const char *str1, const char* str2)
109 {
110  int i;
111  for (i = 0 ; str1[i] != '\0' ; i++) {
112  if (str1[i] != str2[i])
113  return false;
114  }
115 
116  return str2[i] == '\0';
117 }
118 
123 static inline bool strcaseeq(const char *str1, const char* str2)
124 {
125  int i;
126  for (i = 0 ; str1[i] != '\0' ; i++) {
127  if (str1[i] == str2[i])
128  continue;
129  else if (islower(str1[i]) && (str1[i] - 32) == str2[i])
130  continue;
131  else if (isupper(str1[i]) && (str1[i] + 32) == str2[i])
132  continue;
133 
134  return false;
135  }
136 
137  return str2[i] == '\0';
138 }
139 
147 char *str_trim(char *str);
148 
156 char *str_lstrip(char *str);
157 
165 char *str_rstrip(char *str);
166 
172 bool str_ends_with(const char *haystack, const char *needle);
173 
179 bool str_starts_with(const char *haystack, const char *needle);
180 
186 bool str_starts_with_any(const char *haystack, const char **needles, int num_needles);
187 
191 bool str_matches_any(const char *haystack, const char **needles, int num_needles);
192 
207 char *str_substring(const char *str, size_t startidx, long endidx);
208 
215 int str_indexof(const char *haystack, const char *needle);
216 
217  static inline int str_contains(const char *haystack, const char *needle) {
218  return str_indexof(haystack, needle) >= 0;
219  }
220 
221 // same as above, but returns last match
222 int str_last_indexof(const char *haystack, const char *needle);
223 
230 char *str_tolowercase(char *s);
231 
238 char *str_touppercase(char *s);
239 
254 char *str_replace(const char *haystack, const char *needle, const char *replacement);
255 
256  char *str_replace_many(const char *_haystack, ...);
258 // String Buffer
259 
268 
274 
278 void string_buffer_append(string_buffer_t *sb, char c);
279 
285 
289 void string_buffer_append_string(string_buffer_t *sb, const char *str);
290 
295 void string_buffer_appendf(string_buffer_t *sb, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
296 
303 bool string_buffer_ends_with(string_buffer_t *sb, const char *str);
304 
310 
316 
321 
323 // String Feeder
324 
334 string_feeder_t *string_feeder_create(const char *str);
335 
341 
351 
360 
372 char *string_feeder_next_length(string_feeder_t *sf, size_t length);
373 
383 
395 char *string_feeder_peek_length(string_feeder_t *sf, size_t length);
396 
410 
426 
434 bool string_feeder_starts_with(string_feeder_t *sf, const char *str);
435 
443 void string_feeder_require(string_feeder_t *sf, const char *str);
444 
445 /*#ifndef strdup
446  static inline char *strdup(const char *s) {
447  int len = strlen(s);
448  char *out = malloc(len+1);
449  memcpy(out, s, len + 1);
450  return out;
451  }
452 #endif
453 */
454 
455 
456 // find everything that looks like an env variable and expand it
457 // using getenv. Caller should free the result.
458 // e.g. "$HOME/abc" ==> "/home/ebolson/abc"
459 char *str_expand_envs(const char *in);
460 
461 #ifdef __cplusplus
462 }
463 #endif
464 
465 #endif
char * str_touppercase(char *s)
Definition: string_util.c:310
zarray_t * str_split_spaces(const char *str)
Definition: string_util.c:191
void bool string_buffer_ends_with(string_buffer_t *sb, const char *str)
Definition: string_util.c:416
char * _str_concat_private(const char *first,...)
Definition: string_util.c:98
int str_diff_idx(const char *a, const char *b)
Definition: string_util.c:134
char * str_rstrip(char *str)
Definition: string_util.c:249
string_feeder_t * string_feeder_create(const char *str)
Definition: string_util.c:447
char * str_lstrip(char *str)
Definition: string_util.c:237
int string_feeder_get_column(string_feeder_t *sf)
Definition: string_util.c:466
bool str_starts_with(const char *haystack, const char *needle)
Definition: string_util.c:580
char * str_trim(char *str)
Definition: string_util.c:230
void string_buffer_reset(string_buffer_t *sb)
Definition: string_util.c:439
bool str_ends_with(const char *haystack, const char *needle)
Definition: string_util.c:566
static bool strcaseeq(const char *str1, const char *str2)
Definition: string_util.h:123
#define str(s)
char * string_feeder_peek_length(string_feeder_t *sf, size_t length)
Definition: string_util.c:528
void string_buffer_append(string_buffer_t *sb, char c)
Definition: string_util.c:344
string_buffer_t * string_buffer_create()
Definition: string_util.c:323
char string_buffer_pop_back(string_buffer_t *sb)
Definition: string_util.c:357
void string_buffer_appendf(string_buffer_t *sb, const char *fmt,...) __attribute__((format(printf
void string_buffer_append_string(string_buffer_t *sb, const char *str)
Definition: string_util.c:399
size_t string_buffer_size(string_buffer_t *sb)
Definition: string_util.c:432
void str_split_destroy(zarray_t *s)
Definition: string_util.c:221
char * sprintf_alloc(const char *fmt,...) __attribute__((format(printf
static bool streq(const char *str1, const char *str2)
Definition: string_util.h:108
void string_feeder_destroy(string_feeder_t *sf)
Definition: string_util.c:472
char string_feeder_peek(string_feeder_t *sf)
Definition: string_util.c:520
char * str_replace_many(const char *_haystack,...)
Definition: string_util.c:671
char * str_replace(const char *haystack, const char *needle, const char *replacement)
Definition: string_util.c:643
bool str_starts_with_any(const char *haystack, const char **needles, int num_needles)
Definition: string_util.c:596
bool string_feeder_has_next(string_feeder_t *sf)
Definition: string_util.c:482
Definition: zarray.h:49
char char * vsprintf_alloc(const char *fmt, va_list args)
Definition: string_util.c:66
char * str_substring(const char *str, size_t startidx, long endidx)
Definition: string_util.c:626
char * str_expand_envs(const char *in)
Definition: string_util.c:739
int string_feeder_get_line(string_feeder_t *sf)
Definition: string_util.c:460
int str_indexof(const char *haystack, const char *needle)
Definition: string_util.c:259
char string_feeder_next(string_feeder_t *sf)
Definition: string_util.c:489
void string_buffer_destroy(string_buffer_t *sb)
Definition: string_util.c:332
zarray_t * str_split(const char *str, const char *delim)
Definition: string_util.c:154
bool string_feeder_starts_with(string_feeder_t *sf, const char *str)
Definition: string_util.c:542
void string_feeder_require(string_feeder_t *sf, const char *str)
Definition: string_util.c:551
int str_last_indexof(const char *haystack, const char *needle)
Definition: string_util.c:278
char * string_buffer_to_string(string_buffer_t *sb)
Definition: string_util.c:424
char * str_tolowercase(char *s)
Definition: string_util.c:297
char * string_feeder_next_length(string_feeder_t *sf, size_t length)
Definition: string_util.c:505
static int str_contains(const char *haystack, const char *needle)
Definition: string_util.h:217
bool str_matches_any(const char *haystack, const char **needles, int num_needles)
Definition: string_util.c:611


apriltags2
Author(s): Danylo Malyuta
autogenerated on Fri Oct 19 2018 04:02:32