-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_helpers.c
executable file
·202 lines (151 loc) · 5.22 KB
/
string_helpers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/************************************************************************************************************
NAME
string_helpers.c
DESCRIPTION
Helper functions for string manipulations.
AUTHOR
Christian Vigh, 11/2014.
HISTORY
tbd
************************************************************************************************************/
# include "thrak.h"
# include <ctype.h>
# include <math.h>
/*===========================================================================================================
thrak_atoi -
Extended atoi implementation. The output_position parameter can be null.
When escape_only is non-zero, a leading sign is forbidden and the value cannot exceed 255.
*===========================================================================================================*/
THRAK_API zend_bool thrak_atoi ( char * input_value, char ** output_position, int64 * output_value, int escape_only )
{
char * p = input_value ; /* Pointer to current char of input value */
int sign = 0 ; /* Sign (0 = unspecified) */
int base = 10 ; /* Conversion base */
int64 value = 0 ; /* Current conversion value */
int digit_count = 0 ; /* Number of digits processed (0 = conversion error) */
/* Allow leading sign only for real integers, not escape sequences */
if ( ! escape_only )
{
/* Facility : ignore leading spaces */
while ( * p && isspace ( * p ) )
p ++ ;
/* If a sign is present, remember it */
if ( * p == '-' )
sign = -1, p ++ ;
else if ( * p == '+' )
p ++, sign = 1 ;
/* Facility II : ignore spaces between sign and first digits */
while ( * p && isspace ( * p ) )
p ++ ;
}
/* If a number starts with zero, it may be specified in octal... */
if ( * p == '0' )
{
base = 8 ;
p ++ ;
}
/* ... unless the character following is a base specifier */
switch ( tolower ( * p ) )
{
case 'b' :
base = 2 ;
p ++ ;
break ;
case 'o' :
base = 8 ;
p ++ ;
break ;
case 'd' :
base = 10 ;
p ++ ;
break ;
case 'x' :
base = 16 ;
p ++ ;
break ;
}
/* Loop through digits */
while ( isxdigit ( * p ) )
{
char ch = ( * p > '9' ) ? toupper ( * p ) - 'A' + 10 : * p - '0' ;
int64 tmpval ;
/* Stop on first digit not belonging to the current base */
if ( ch >= base )
break ;
/* If we interpret an escaped character sequence, make sure we don't overflow */
tmpval = ( value * base ) + ch ;
if ( escape_only && tmpval > 255 )
break ;
/* Proceed with next character */
value = tmpval ;
digit_count ++ ;
p ++ ;
}
/* No digit found : signal a conversion error */
if ( ! digit_count )
{
if ( output_position != NULL )
* output_position = input_value ;
return ( 0 ) ;
}
/* If an output position pointer has been supplied, set it to the first character after the last digit */
if ( output_position != NULL )
* output_position = p ;
/* Don't forget the leading sign, if any */
if ( sign )
value *= sign ;
/* All done, return */
* output_value = value ;
return ( 1 ) ;
}
/*===========================================================================================================
thrak_strichr -
strichr() implementation for systems that don't support it/
*===========================================================================================================*/
THRAK_API char * thrak_strichr ( const char * p, int ch )
{
ch = toupper ( ch ) ;
while ( * p )
{
if ( toupper ( * p ) == ch )
return ( ( char * ) p ) ;
p ++ ;
}
return ( NULL ) ;
}
/*===========================================================================================================
thrak_trim/ltrim/rtrim -
Trims a value. Puts the result in the "output" parameter if specified, or returns a newly
allocated string.
*===========================================================================================================*/
static char * __thrak_trim ( char * p, int mode, char * output )
{
char * start = p,
* end = p + strlen ( p ) - 1 ;
long size ;
if ( mode & 1 )
{
while ( * start && isspace ( * start ) )
start ++ ;
}
if ( mode & 2 )
{
while ( end > start && isspace ( * end ) )
end -- ;
}
size = ( long ) ( end - start + 1 ) ;
if ( output != NULL )
{
strncpy ( output, start, size ) ;
* ( end + 1 ) = '\0' ;
return ( output ) ;
}
else
return ( estrndup ( start, size ) ) ;
}
THRAK_API char * thrak_trim ( char * p, char * output )
{ return ( __thrak_trim ( p, 0x03, output ) ) ; }
THRAK_API char * thrak_ltrim ( char * p, char * output )
{ return ( __thrak_trim ( p, 0x01, output ) ) ; }
THRAK_API char * thrak_rtrim ( char * p, char * output )
{ return ( __thrak_trim ( p, 0x02, output ) ) ; }