Rizin
unix-like reverse engineering framework and cli tools
str_trim.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2007-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "rz_types.h"
5 #include "rz_util.h"
6 #include "rz_cons.h"
7 #include "rz_bin.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include <stdarg.h>
13 
14 // TODO: simplify this horrible loop
15 RZ_API void rz_str_trim_path(char *s) {
16  char *src, *dst, *p;
17  int i = 0;
18  if (!s || !*s) {
19  return;
20  }
21  dst = src = s + 1;
22  while (*src) {
23  if (*(src - 1) == '/' && *src == '.' && *(src + 1) == '.') {
24  if (*(src + 2) == '/' || *(src + 2) == '\0') {
25  p = dst - 1;
26  while (s != p) {
27  if (*p == '/') {
28  if (i) {
29  dst = p + 1;
30  i = 0;
31  break;
32  }
33  i = 1;
34  }
35  p--;
36  }
37  if (s == p && *p == '/') {
38  dst = p + 1;
39  }
40  src = src + 2;
41  } else {
42  *dst = *src;
43  dst++;
44  }
45  } else if (*src == '/' && *(src + 1) == '.' && (*(src + 2) == '/' || *(src + 2) == '\0')) {
46  src++;
47  } else if (*src != '/' || *(src - 1) != '/') {
48  *dst = *src;
49  dst++;
50  }
51  src++;
52  }
53  if (dst > s + 1 && *(dst - 1) == '/') {
54  *(dst - 1) = 0;
55  } else {
56  *dst = 0;
57  }
58 }
59 
61  RzList *list = rz_str_split_list(str, "\n", 0);
62  char *s;
64  RzStrBuf *sb = rz_strbuf_new("");
65  rz_list_foreach (list, iter, s) {
66  // rz_str_ansi_trim (s, -1, 99999);
68  rz_str_trim(s);
69  if (*s) {
70  rz_strbuf_appendf(sb, "%s\n", s);
71  }
72  }
74  free(str);
75  return rz_strbuf_drain(sb);
76 }
77 
78 RZ_API char *rz_str_trim_dup(const char *str) {
79  char *a = strdup(str);
80  rz_str_trim(a);
81  return a;
82 }
83 
84 // Returns a pointer to the first non-whitespace character of str.
85 // TODO: Find a better name: to rz_str_trim_head_ro(), rz_str_skip_head or so
86 RZ_API const char *rz_str_trim_head_ro(const char *str) {
88  for (; *str && IS_WHITECHAR(*str); str++) {
89  ;
90  }
91  return str;
92 }
93 
94 // TODO: find better name
95 RZ_API const char *rz_str_trim_head_wp(const char *str) {
97  for (; *str && !IS_WHITESPACE(*str); str++) {
98  ;
99  }
100  return str;
101 }
102 
111  char *p = (char *)rz_str_trim_head_ro(str);
112  if (p) {
113  memmove(str, p, strlen(p) + 1);
114  }
115 }
116 
127  size_t length = strlen(str);
128  while (length-- > 0) {
129  if (IS_WHITECHAR(str[length])) {
130  str[length] = '\0';
131  } else {
132  break;
133  }
134  }
135  return str;
136 }
137 
146  char *p = str;
147  for (; *p && (*p == c); p++) {
148  ;
149  }
150  if (p) {
151  memmove(str, p, strlen(p) + 1);
152  }
153 }
154 
163  size_t length = strlen(str);
164  while (length-- > 0) {
165  if (str[length] == c) {
166  str[length] = '\0';
167  } else {
168  break;
169  }
170  }
171 }
172 
179 RZ_API void rz_str_trim_char(RZ_NONNULL RZ_INOUT char *str, const char c) {
182 }
183 
193 }
194 
195 // no copy, like trim_head+tail but with trim_head_ro, beware heap issues
196 // TODO: rename to rz_str_trim_weak() ?
197 RZ_API char *rz_str_trim_nc(char *str) {
198  char *s = (char *)rz_str_trim_head_ro(str);
200  return s;
201 }
202 
203 /* supposed to chop a string with ansi controls to max length of n. */
204 RZ_API int rz_str_ansi_trim(char *str, int str_len, int n) {
206  char ch, ch2;
207  int back = 0, i = 0, len = 0;
208  /* simple case - no need to cut */
209  if (str_len < 0) {
210  str_len = strlen(str);
211  }
212  if (n >= str_len) {
213  str[str_len] = 0;
214  return str_len;
215  }
216  while ((i < str_len) && str[i] && len < n && n > 0) {
217  ch = str[i];
218  ch2 = str[i + 1];
219  if (ch == 0x1b) {
220  if (ch2 == '\\') {
221  i++;
222  } else if (ch2 == ']') {
223  if (!strncmp(str + 2 + 5, "rgb:", 4)) {
224  i += 18;
225  }
226  } else if (ch2 == '[') {
227  for (++i; (i < str_len) && str[i] && str[i] != 'J' && str[i] != 'm' && str[i] != 'H';
228  i++) {
229  ;
230  }
231  }
232  } else if ((str[i] & 0xc0) != 0x80) {
233  len++;
234  }
235  i++;
236  back = i; /* index in the original array */
237  }
238  str[back] = 0;
239  return back;
240 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
lzma_index * src
Definition: index.h:567
static SblHeader sb
Definition: bin_mbn.c:26
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * p
Definition: libc.cpp:67
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
char * dst
Definition: lz4.h:724
int n
Definition: mipsasm.c:19
static RzSocket * s
Definition: rtr.c:28
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API int rz_str_ansi_filter(char *str, char **out, int **cposs, int len)
Definition: str.c:2124
RZ_API RzList * rz_str_split_list(char *str, const char *c, int n)
Split the string str according to the substring c and returns a RzList with the result.
Definition: str.c:3429
#define IS_WHITESPACE(x)
Definition: rz_str_util.h:13
#define IS_WHITECHAR(x)
Definition: rz_str_util.h:5
RZ_API RZ_OWN char * rz_strbuf_drain(RzStrBuf *sb)
Definition: strbuf.c:342
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_INOUT
Definition: rz_types.h:52
#define RZ_BORROW
Definition: rz_types.h:63
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41
RZ_API void rz_str_trim_char(RZ_NONNULL RZ_INOUT char *str, const char c)
Removes the character c from the beginning and end of a string.
Definition: str_trim.c:179
RZ_API char * rz_str_trim_lines(char *str)
Definition: str_trim.c:60
RZ_API void rz_str_trim_head_char(RZ_NONNULL RZ_INOUT char *str, const char c)
Removes the the character c from the beginning of a string.
Definition: str_trim.c:144
RZ_API RZ_BORROW char * rz_str_trim_tail(RZ_NONNULL char *str)
Removes whitespace characters (space, tab, newline etc.) from the end of a string and replaces them w...
Definition: str_trim.c:125
RZ_API const char * rz_str_trim_head_wp(const char *str)
Definition: str_trim.c:95
RZ_API char * rz_str_trim_dup(const char *str)
Definition: str_trim.c:78
RZ_API void rz_str_trim_head(RZ_NONNULL char *str)
Removes whitespace characters (space, tab, newline etc.) from the end of a string....
Definition: str_trim.c:110
RZ_API void rz_str_trim_tail_char(RZ_NONNULL RZ_INOUT char *str, const char c)
Removes the the character c from the end of a string.
Definition: str_trim.c:161
RZ_API int rz_str_ansi_trim(char *str, int str_len, int n)
Definition: str_trim.c:204
RZ_API const char * rz_str_trim_head_ro(const char *str)
Definition: str_trim.c:86
RZ_API void rz_str_trim(RZ_NONNULL RZ_INOUT char *str)
Removes whitespace characters (space, tab, newline etc.) from the beginning and end of a string.
Definition: str_trim.c:190
RZ_API void rz_str_trim_path(char *s)
Definition: str_trim.c:15
RZ_API char * rz_str_trim_nc(char *str)
Definition: str_trim.c:197