Rizin
unix-like reverse engineering framework and cli tools
str_trim.c File Reference
#include "rz_types.h"
#include "rz_util.h"
#include "rz_cons.h"
#include "rz_bin.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>

Go to the source code of this file.

Functions

RZ_API void rz_str_trim_path (char *s)
 
RZ_API char * rz_str_trim_lines (char *str)
 
RZ_API char * rz_str_trim_dup (const char *str)
 
RZ_API const char * rz_str_trim_head_ro (const char *str)
 
RZ_API const char * rz_str_trim_head_wp (const char *str)
 
RZ_API void rz_str_trim_head (RZ_NONNULL char *str)
 Removes whitespace characters (space, tab, newline etc.) from the end of a string. The string is changed in place. More...
 
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 with '\0' characters. The string is changed in place. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
RZ_API char * rz_str_trim_nc (char *str)
 
RZ_API int rz_str_ansi_trim (char *str, int str_len, int n)
 

Function Documentation

◆ rz_str_ansi_trim()

RZ_API int rz_str_ansi_trim ( char *  str,
int  str_len,
int  n 
)

Definition at line 204 of file str_trim.c.

204  {
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
int n
Definition: mipsasm.c:19
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108

References i, len, n, rz_return_val_if_fail, and cmd_descs_generate::str.

Referenced by rz_strpool_ansi_chop().

◆ rz_str_trim()

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.

Parameters
strThe string to trim.

Definition at line 190 of file str_trim.c.

190  {
193 }
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 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

References rz_str_trim_head(), rz_str_trim_tail(), and cmd_descs_generate::str.

Referenced by __op_refs(), __refs(), __reg_read(), addVar(), assign_reg(), cmd_print_pv(), disasm_strings(), do_handle_substitution_cmd(), ds_print_comments_right(), ds_print_dwarf(), exprmatchreg(), filter_item_name(), function_print_to_json(), get_body(), get_long_object(), getpcfromstack(), handleHints(), interact_commands(), parse(), prim(), print_fcn_arg(), rz_analysis_cc_set(), rz_asm_massemble(), rz_bin_addr2text(), rz_bin_elf_get_compiler(), rz_cons_grep_parsecmd(), rz_cons_grep_strip(), rz_core_asm_strsearch(), rz_core_cmd_subst_i(), rz_core_rtr_add(), rz_core_visual_cmd(), rz_name_filter(), rz_str_trim_dup(), rz_str_trim_lines(), rz_str_version(), rz_syscmd_cat(), rz_syscmd_head(), rz_syscmd_join(), rz_syscmd_mkdir(), rz_syscmd_sort(), rz_syscmd_tail(), rz_syscmd_uniq(), rz_test_load_asm_test_file(), rz_test_load_json_test_file(), rz_test_run_asm_test(), rz_time_stamp_to_str(), rz_w32_add_winmsg_breakpoint(), socket_http_answer(), str_split_list_common(), and str_split_list_common_regex().

◆ rz_str_trim_char()

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.

Parameters
strThe string to trim.
cThe character to remove.

Definition at line 179 of file str_trim.c.

179  {
182 }
#define c(i)
Definition: sha256.c:43
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 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

References c, rz_str_trim_head_char(), rz_str_trim_tail_char(), and cmd_descs_generate::str.

Referenced by ds_build_op_str().

◆ rz_str_trim_dup()

RZ_API char* rz_str_trim_dup ( const char *  str)

Definition at line 78 of file str_trim.c.

78  {
79  char *a = strdup(str);
80  rz_str_trim(a);
81  return a;
82 }
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")
#define a(i)
Definition: sha256.c:41
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

References a, rz_str_trim(), cmd_descs_generate::str, and strdup().

Referenced by capture_filter_keywords(), get_corefile_name(), rz_cmd_remote(), rz_cmd_search(), rz_config_eval(), rz_core_asm_strsearch(), rz_core_cmd_foreach3(), rz_regs_fpu_handler(), and rz_remote_mode_enable_handler().

◆ rz_str_trim_head()

RZ_API void rz_str_trim_head ( RZ_NONNULL char *  str)

Removes whitespace characters (space, tab, newline etc.) from the end of a string. The string is changed in place.

Parameters
strThe string to trim.

Definition at line 110 of file str_trim.c.

110  {
111  char *p = (char *)rz_str_trim_head_ro(str);
112  if (p) {
113  memmove(str, p, strlen(p) + 1);
114  }
115 }
void * p
Definition: libc.cpp:67
RZ_API const char * rz_str_trim_head_ro(const char *str)
Definition: str_trim.c:86

References p, rz_str_trim_head_ro(), and cmd_descs_generate::str.

Referenced by parse(), rz_core_cmd_pipe_old(), rz_core_cmd_subst_i(), and rz_str_trim().

◆ rz_str_trim_head_char()

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.

Parameters
strThe string.
cThe character to be removed.

Definition at line 144 of file str_trim.c.

144  {
146  char *p = str;
147  for (; *p && (*p == c); p++) {
148  ;
149  }
150  if (p) {
151  memmove(str, p, strlen(p) + 1);
152  }
153 }
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100

References c, p, rz_return_if_fail, and cmd_descs_generate::str.

Referenced by rz_str_trim_char().

◆ rz_str_trim_head_ro()

RZ_API const char* rz_str_trim_head_ro ( const char *  str)

Definition at line 86 of file str_trim.c.

86  {
88  for (; *str && IS_WHITECHAR(*str); str++) {
89  ;
90  }
91  return str;
92 }
#define NULL
Definition: cris-opc.c:27
#define IS_WHITECHAR(x)
Definition: rz_str_util.h:5

References IS_WHITECHAR, NULL, rz_return_val_if_fail, and cmd_descs_generate::str.

Referenced by __system(), autocomplete_file(), autocomplete_sdb(), autocompleteFilename(), cmd_analysis_esil(), cmd_debug_pid(), cmd_print_format(), cmd_print_pv(), cmd_pxr(), cmp(), core_cmd_tsrzcmd(), do_analysis_search(), encode_const_pool16_ut8(), encode_ut8x2(), find_autocomplete(), flag_to_flag(), gdb_to_rz_profile(), get_section_name(), is_macro_command(), java_assembler(), lua53_assembly(), lua54_assembly(), lua_load_next_arg_start(), old_transform_op(), prompt_offset_and_seek(), prompt_offset_and_seek_file(), rz_analysis_noreturn_drop(), rz_asn1_print_padded(), rz_cmd_analysis(), rz_cmd_debug(), rz_cmd_debug_dmi(), rz_cmd_help(), rz_cmd_print(), rz_cmd_search(), rz_cons_hud_path(), rz_cons_pal_list(), rz_core_agraph_print(), rz_core_analysis_function_rename(), rz_core_autocomplete(), rz_core_cmd_subst(), rz_core_cmd_subst_i(), rz_core_graph_print(), rz_core_rtr_add(), rz_core_rtr_cmd(), rz_core_rtr_gdb_run(), rz_core_visual_view_rop(), rz_core_visual_xrefs(), rz_equal_H_handler_old(), rz_plugins_load_handler(), rz_plugins_unload_handler(), rz_print_json_indent(), rz_reg_arena_set_bytes(), rz_str_trim_head(), rz_str_trim_nc(), rz_str_version(), rz_str_wrap(), rz_syscmd_ls(), rz_syscmd_mkdir(), rz_table_query(), step_until_inst(), subvar(), tokens_new(), and types_cc_print().

◆ rz_str_trim_head_wp()

RZ_API const char* rz_str_trim_head_wp ( const char *  str)

Definition at line 95 of file str_trim.c.

95  {
97  for (; *str && !IS_WHITESPACE(*str); str++) {
98  ;
99  }
100  return str;
101 }
#define IS_WHITESPACE(x)
Definition: rz_str_util.h:13

References IS_WHITESPACE, NULL, rz_return_val_if_fail, and cmd_descs_generate::str.

Referenced by find_autocomplete(), and tokens_new().

◆ rz_str_trim_lines()

RZ_API char* rz_str_trim_lines ( char *  str)

Definition at line 60 of file str_trim.c.

60  {
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 }
static SblHeader sb
Definition: bin_mbn.c:26
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
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
static RzSocket * s
Definition: rtr.c:28
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
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

References free(), list(), NULL, rz_list_free(), rz_str_ansi_filter(), rz_str_split_list(), rz_str_trim(), rz_strbuf_appendf(), rz_strbuf_drain(), rz_strbuf_new(), s, sb, and cmd_descs_generate::str.

Referenced by get_body().

◆ rz_str_trim_nc()

RZ_API char* rz_str_trim_nc ( char *  str)

Definition at line 197 of file str_trim.c.

197  {
198  char *s = (char *)rz_str_trim_head_ro(str);
200  return s;
201 }

References rz_str_trim_head_ro(), rz_str_trim_tail(), s, and cmd_descs_generate::str.

Referenced by rz_core_cmd_subst_i().

◆ rz_str_trim_path()

RZ_API void rz_str_trim_path ( char *  s)

Definition at line 15 of file str_trim.c.

15  {
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 }
lzma_index * src
Definition: index.h:567
char * dst
Definition: lz4.h:724

References dst, i, p, s, and src.

◆ rz_str_trim_tail()

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 with '\0' characters. The string is changed in place.

Parameters
strThe string to trim.
Returns
The edited string.

Definition at line 125 of file str_trim.c.

125  {
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 }
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

References IS_WHITECHAR, length, rz_return_val_if_fail, and cmd_descs_generate::str.

Referenced by ar_parse_header(), arm_opcode_parse(), get_section_name(), getstr(), rz_analysis_xrefs_to_graph_cmd_handler(), rz_base_type_as_format(), rz_cons_canvas_print_region(), rz_cons_fgets(), rz_core_analysis_function_set_signature(), rz_core_analysis_hasrefs_to_depth(), rz_core_bin_fields_print(), rz_core_cmd_subst(), rz_core_cmd_subst_i(), rz_il_validate_effect(), rz_il_validate_pure(), rz_line_hist_load(), rz_str_trim(), rz_str_trim_nc(), and rz_type_as_format_pair().

◆ rz_str_trim_tail_char()

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.

Parameters
strThe string.
cThe character to be removed.

Definition at line 161 of file str_trim.c.

161  {
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 }

References c, length, rz_return_if_fail, and cmd_descs_generate::str.

Referenced by rz_str_trim_char().