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 This software was developed in the APRIL Robotics Lab under the
4 direction of Edwin Olson, ebolson@umich.edu. This software may be
5 available under alternative licensing terms; contact the address above.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 The views and conclusions contained in the software and documentation are those
24 of the authors and should not be interpreted as representing official policies,
25 either expressed or implied, of the Regents of The University of Michigan.
26 */
27 
28 #pragma once
29 
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <stdbool.h>
33 #include <ctype.h>
34 
35 #include "zarray.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 
43 
46 {
47  char *s;
48  size_t len;
49  size_t pos;
50 
51  int line, col;
52 };
53 
59 char *sprintf_alloc(const char *fmt, ...)
60 #ifndef _MSC_VER
61 __attribute__ ((format (printf, 1, 2)))
62 #endif
63 ;
64 
70 char *vsprintf_alloc(const char *fmt, va_list args);
71 
76 #define str_concat(...) _str_concat_private(__VA_ARGS__, NULL)
77 char *_str_concat_private(const char *first, ...);
78 
79 
80 // Returns the index of the first character that differs:
81 int str_diff_idx(const char * a, const char * b);
82 
97 zarray_t *str_split(const char *str, const char *delim);
98 
99 zarray_t *str_split_spaces(const char *str);
100 
101 void str_split_destroy(zarray_t *s);
102 
103 /*
104  * Determines if str1 exactly matches str2 (more efficient than strcmp(...) == 0)
105  */
106 static inline bool streq(const char *str1, const char* str2)
107 {
108  int i;
109  for (i = 0 ; str1[i] != '\0' ; i++) {
110  if (str1[i] != str2[i])
111  return false;
112  }
113 
114  return str2[i] == '\0';
115 }
116 
121 static inline bool strcaseeq(const char *str1, const char* str2)
122 {
123  int i;
124  for (i = 0 ; str1[i] != '\0' ; i++) {
125  if (str1[i] == str2[i])
126  continue;
127  else if (islower(str1[i]) && (str1[i] - 32) == str2[i])
128  continue;
129  else if (isupper(str1[i]) && (str1[i] + 32) == str2[i])
130  continue;
131 
132  return false;
133  }
134 
135  return str2[i] == '\0';
136 }
137 
145 char *str_trim(char *str);
146 
154 char *str_lstrip(char *str);
155 
163 char *str_rstrip(char *str);
164 
170 bool str_ends_with(const char *haystack, const char *needle);
171 
177 bool str_starts_with(const char *haystack, const char *needle);
178 
184 bool str_starts_with_any(const char *haystack, const char **needles, int num_needles);
185 
189 bool str_matches_any(const char *haystack, const char **needles, int num_needles);
190 
205 char *str_substring(const char *str, size_t startidx, size_t endidx);
206 
213 int str_indexof(const char *haystack, const char *needle);
214 
215  static inline int str_contains(const char *haystack, const char *needle) {
216  return str_indexof(haystack, needle) >= 0;
217  }
218 
219 // same as above, but returns last match
220 int str_last_indexof(const char *haystack, const char *needle);
221 
228 char *str_tolowercase(char *s);
229 
236 char *str_touppercase(char *s);
237 
252 char *str_replace(const char *haystack, const char *needle, const char *replacement);
253 
254  char *str_replace_many(const char *_haystack, ...);
256 // String Buffer
257 
266 
272 
276 void string_buffer_append(string_buffer_t *sb, char c);
277 
283 
287 void string_buffer_append_string(string_buffer_t *sb, const char *str);
288 
293 void string_buffer_appendf(string_buffer_t *sb, const char *fmt, ...)
294 #ifndef _MSC_VER
295 __attribute__ ((format (printf, 2, 3)))
296 #endif
297 ;
298 
305 bool string_buffer_ends_with(string_buffer_t *sb, const char *str);
306 
312 
318 
323 
325 // String Feeder
326 
336 string_feeder_t *string_feeder_create(const char *str);
337 
343 
353 
362 
374 char *string_feeder_next_length(string_feeder_t *sf, size_t length);
375 
385 
397 char *string_feeder_peek_length(string_feeder_t *sf, size_t length);
398 
412 
428 
436 bool string_feeder_starts_with(string_feeder_t *sf, const char *str);
437 
445 void string_feeder_require(string_feeder_t *sf, const char *str);
446 
447 /*#ifndef strdup
448  static inline char *strdup(const char *s) {
449  int len = strlen(s);
450  char *out = malloc(len+1);
451  memcpy(out, s, len + 1);
452  return out;
453  }
454 #endif
455 */
456 
457 
458 // find everything that looks like an env variable and expand it
459 // using getenv. Caller should free the result.
460 // e.g. "$HOME/abc" ==> "/home/ebolson/abc"
461 char *str_expand_envs(const char *in);
462 
463 #ifdef __cplusplus
464 }
465 #endif
vsprintf_alloc
char char * vsprintf_alloc(const char *fmt, va_list args)
Definition: string_util.c:60
str_last_indexof
int str_last_indexof(const char *haystack, const char *needle)
Definition: string_util.c:272
str_expand_envs
char * str_expand_envs(const char *in)
Definition: string_util.c:732
string_feeder_peek_length
char * string_feeder_peek_length(string_feeder_t *sf, size_t length)
Definition: string_util.c:521
str_tolowercase
char * str_tolowercase(char *s)
Definition: string_util.c:291
str_split_destroy
void str_split_destroy(zarray_t *s)
Definition: string_util.c:215
str_indexof
int str_indexof(const char *haystack, const char *needle)
Definition: string_util.c:253
string_buffer_create
string_buffer_t * string_buffer_create()
Definition: string_util.c:317
str_split
zarray_t * str_split(const char *str, const char *delim)
Definition: string_util.c:148
string_buffer_pop_back
char string_buffer_pop_back(string_buffer_t *sb)
Definition: string_util.c:351
string_feeder::len
size_t len
Definition: string_util.h:48
string_feeder_starts_with
bool string_feeder_starts_with(string_feeder_t *sf, const char *str)
Definition: string_util.c:534
string_feeder_require
void string_feeder_require(string_feeder_t *sf, const char *str)
Definition: string_util.c:543
string_feeder_destroy
void string_feeder_destroy(string_feeder_t *sf)
Definition: string_util.c:466
string_buffer_append_string
void string_buffer_append_string(string_buffer_t *sb, const char *str)
Definition: string_util.c:393
string_buffer_to_string
char * string_buffer_to_string(string_buffer_t *sb)
Definition: string_util.c:418
string_feeder_next_length
char * string_feeder_next_length(string_feeder_t *sf, size_t length)
Definition: string_util.c:499
format
char * format(const char *fmt,...)
Definition: test_detection.c:11
sprintf_alloc
char * sprintf_alloc(const char *fmt,...) __attribute__((format(printf
zarray
Definition: zarray.h:43
streq
static bool streq(const char *str1, const char *str2)
Definition: string_util.h:106
string_buffer
Definition: string_util.c:38
str_split_spaces
zarray_t * str_split_spaces(const char *str)
Definition: string_util.c:185
string_feeder_peek
char string_feeder_peek(string_feeder_t *sf)
Definition: string_util.c:513
string_feeder
Definition: string_util.h:45
str_touppercase
char * str_touppercase(char *s)
Definition: string_util.c:304
str_starts_with_any
bool str_starts_with_any(const char *haystack, const char **needles, int num_needles)
Definition: string_util.c:592
str_diff_idx
int str_diff_idx(const char *a, const char *b)
Definition: string_util.c:128
string_feeder_next
char string_feeder_next(string_feeder_t *sf)
Definition: string_util.c:483
str_substring
char * str_substring(const char *str, size_t startidx, size_t endidx)
Definition: string_util.c:622
string_feeder::pos
size_t pos
Definition: string_util.h:49
str_lstrip
char * str_lstrip(char *str)
Definition: string_util.c:231
str_rstrip
char * str_rstrip(char *str)
Definition: string_util.c:243
string_buffer_destroy
void string_buffer_destroy(string_buffer_t *sb)
Definition: string_util.c:326
string_buffer_reset
void string_buffer_reset(string_buffer_t *sb)
Definition: string_util.c:433
string_feeder_create
string_feeder_t * string_feeder_create(const char *str)
Definition: string_util.c:441
str_ends_with
bool str_ends_with(const char *haystack, const char *needle)
Definition: string_util.c:559
str_starts_with
bool str_starts_with(const char *haystack, const char *needle)
Definition: string_util.c:576
string_buffer_append
void string_buffer_append(string_buffer_t *sb, char c)
Definition: string_util.c:338
strcaseeq
static bool strcaseeq(const char *str1, const char *str2)
Definition: string_util.h:121
str_contains
static int str_contains(const char *haystack, const char *needle)
Definition: string_util.h:215
string_feeder::line
int line
Definition: string_util.h:51
string_feeder::s
char * s
Definition: string_util.h:47
str_matches_any
bool str_matches_any(const char *haystack, const char **needles, int num_needles)
Definition: string_util.c:607
string_buffer_size
size_t string_buffer_size(string_buffer_t *sb)
Definition: string_util.c:426
string_buffer_ends_with
void bool string_buffer_ends_with(string_buffer_t *sb, const char *str)
Definition: string_util.c:410
_str_concat_private
char * _str_concat_private(const char *first,...)
Definition: string_util.c:92
string_feeder::col
int col
Definition: string_util.h:51
string_buffer_appendf
void string_buffer_appendf(string_buffer_t *sb, const char *fmt,...) __attribute__((format(printf
zarray.h
string_feeder_get_column
int string_feeder_get_column(string_feeder_t *sf)
Definition: string_util.c:460
str_replace
char * str_replace(const char *haystack, const char *needle, const char *replacement)
Definition: string_util.c:636
str_trim
char * str_trim(char *str)
Definition: string_util.c:224
string_feeder_has_next
bool string_feeder_has_next(string_feeder_t *sf)
Definition: string_util.c:476
str_replace_many
char * str_replace_many(const char *_haystack,...)
Definition: string_util.c:664
string_feeder_get_line
int string_feeder_get_line(string_feeder_t *sf)
Definition: string_util.c:454


apriltag
Author(s): Edwin Olson , Max Krogius
autogenerated on Sun Apr 20 2025 02:08:19