1
/* $Id: string.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <pj/string.h>
21
#include <pj/assert.h>
27
#if PJ_FUNCTIONS_ARE_INLINED==0
28
# include <pj/string_i.h>
32
PJ_DEF(char*) pj_strstr(const pj_str_t *str, const pj_str_t *substr)
36
/* Special case when substr is zero */
37
if (substr->slen == 0) {
38
return (char*)str->ptr;
42
ends = str->ptr + str->slen - substr->slen;
43
for (; s<=ends; ++s) {
44
if (pj_ansi_strncmp(s, substr->ptr, substr->slen)==0)
51
PJ_DEF(char*) pj_stristr(const pj_str_t *str, const pj_str_t *substr)
55
/* Special case when substr is zero */
56
if (substr->slen == 0) {
57
return (char*)str->ptr;
61
ends = str->ptr + str->slen - substr->slen;
62
for (; s<=ends; ++s) {
63
if (pj_ansi_strnicmp(s, substr->ptr, substr->slen)==0)
70
PJ_DEF(pj_str_t*) pj_strltrim( pj_str_t *str )
72
char *end = str->ptr + str->slen;
73
register char *p = str->ptr;
74
while (p < end && pj_isspace(*p))
76
str->slen -= (p - str->ptr);
81
PJ_DEF(pj_str_t*) pj_strrtrim( pj_str_t *str )
83
char *end = str->ptr + str->slen;
84
register char *p = end - 1;
85
while (p >= str->ptr && pj_isspace(*p))
87
str->slen -= ((end - p) - 1);
91
PJ_DEF(char*) pj_create_random_string(char *str, pj_size_t len)
98
for (i=0; i<len/8; ++i) {
99
pj_uint32_t val = pj_rand();
100
pj_val_to_hex_digit( (val & 0xFF000000) >> 24, p+0 );
101
pj_val_to_hex_digit( (val & 0x00FF0000) >> 16, p+2 );
102
pj_val_to_hex_digit( (val & 0x0000FF00) >> 8, p+4 );
103
pj_val_to_hex_digit( (val & 0x000000FF) >> 0, p+6 );
106
for (i=i * 8; i<len; ++i) {
107
*p++ = pj_hex_digits[ pj_rand() & 0x0F ];
113
PJ_DEF(unsigned long) pj_strtoul(const pj_str_t *str)
121
for (i=0; i<(unsigned)str->slen; ++i) {
122
if (!pj_isdigit(str->ptr[i]))
124
value = value * 10 + (str->ptr[i] - '0');
129
PJ_DEF(unsigned long) pj_strtoul2(const pj_str_t *str, pj_str_t *endptr,
139
for (i=0; i<(unsigned)str->slen; ++i) {
140
unsigned c = (str->ptr[i] - '0');
143
value = value * base + c;
145
} else if (base == 16) {
146
for (i=0; i<(unsigned)str->slen; ++i) {
147
if (!pj_isxdigit(str->ptr[i]))
149
value = value * 16 + pj_hex_digit_to_val(str->ptr[i]);
152
pj_assert(!"Unsupported base");
154
value = 0xFFFFFFFFUL;
158
endptr->ptr = str->ptr + i;
159
endptr->slen = str->slen - i;
165
PJ_DEF(int) pj_utoa(unsigned long val, char *buf)
167
return pj_utoa_pad(val, buf, 0, 0);
170
PJ_DEF(int) pj_utoa_pad( unsigned long val, char *buf, int min_dig, int pad)
179
unsigned long digval = (unsigned long) (val % 10);
181
*p++ = (char) (digval + '0');
185
while (len < min_dig) {