Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
3253105d
Commit
3253105d
authored
Sep 20, 2010
by
Pieter Noordhuis
Browse files
Update sds code
parent
e621f313
Changes
2
Show whitespace changes
Inline
Side-by-side
sds.c
View file @
3253105d
...
@@ -33,7 +33,6 @@
...
@@ -33,7 +33,6 @@
#include "sds.h"
#include "sds.h"
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <string.h>
#include <ctype.h>
#include <ctype.h>
...
@@ -155,8 +154,8 @@ sds sdscpy(sds s, char *t) {
...
@@ -155,8 +154,8 @@ sds sdscpy(sds s, char *t) {
return
sdscpylen
(
s
,
t
,
strlen
(
t
));
return
sdscpylen
(
s
,
t
,
strlen
(
t
));
}
}
sds
sdscatprintf
(
sds
s
,
const
char
*
fmt
,
...
)
{
sds
sdscat
v
printf
(
sds
s
,
const
char
*
fmt
,
va_list
ap
)
{
va_list
ap
;
va_list
cpy
;
char
*
buf
,
*
t
;
char
*
buf
,
*
t
;
size_t
buflen
=
16
;
size_t
buflen
=
16
;
...
@@ -168,9 +167,8 @@ sds sdscatprintf(sds s, const char *fmt, ...) {
...
@@ -168,9 +167,8 @@ sds sdscatprintf(sds s, const char *fmt, ...) {
if
(
buf
==
NULL
)
return
NULL
;
if
(
buf
==
NULL
)
return
NULL
;
#endif
#endif
buf
[
buflen
-
2
]
=
'\0'
;
buf
[
buflen
-
2
]
=
'\0'
;
va_start
(
ap
,
fmt
);
va_copy
(
cpy
,
ap
);
vsnprintf
(
buf
,
buflen
,
fmt
,
ap
);
vsnprintf
(
buf
,
buflen
,
fmt
,
cpy
);
va_end
(
ap
);
if
(
buf
[
buflen
-
2
]
!=
'\0'
)
{
if
(
buf
[
buflen
-
2
]
!=
'\0'
)
{
free
(
buf
);
free
(
buf
);
buflen
*=
2
;
buflen
*=
2
;
...
@@ -183,6 +181,15 @@ sds sdscatprintf(sds s, const char *fmt, ...) {
...
@@ -183,6 +181,15 @@ sds sdscatprintf(sds s, const char *fmt, ...) {
return
t
;
return
t
;
}
}
sds
sdscatprintf
(
sds
s
,
const
char
*
fmt
,
...)
{
va_list
ap
;
char
*
t
;
va_start
(
ap
,
fmt
);
t
=
sdscatvprintf
(
s
,
fmt
,
ap
);
va_end
(
ap
);
return
t
;
}
sds
sdstrim
(
sds
s
,
const
char
*
cset
)
{
sds
sdstrim
(
sds
s
,
const
char
*
cset
)
{
struct
sdshdr
*
sh
=
(
void
*
)
(
s
-
(
sizeof
(
struct
sdshdr
)));
struct
sdshdr
*
sh
=
(
void
*
)
(
s
-
(
sizeof
(
struct
sdshdr
)));
char
*
start
,
*
end
,
*
sp
,
*
ep
;
char
*
start
,
*
end
,
*
sp
,
*
ep
;
...
@@ -200,7 +207,7 @@ sds sdstrim(sds s, const char *cset) {
...
@@ -200,7 +207,7 @@ sds sdstrim(sds s, const char *cset) {
return
s
;
return
s
;
}
}
sds
sdsrange
(
sds
s
,
long
start
,
long
end
)
{
sds
sdsrange
(
sds
s
,
int
start
,
int
end
)
{
struct
sdshdr
*
sh
=
(
void
*
)
(
s
-
(
sizeof
(
struct
sdshdr
)));
struct
sdshdr
*
sh
=
(
void
*
)
(
s
-
(
sizeof
(
struct
sdshdr
)));
size_t
newlen
,
len
=
sdslen
(
s
);
size_t
newlen
,
len
=
sdslen
(
s
);
...
@@ -356,3 +363,117 @@ sds sdsfromlonglong(long long value) {
...
@@ -356,3 +363,117 @@ sds sdsfromlonglong(long long value) {
p
++
;
p
++
;
return
sdsnewlen
(
p
,
32
-
(
p
-
buf
));
return
sdsnewlen
(
p
,
32
-
(
p
-
buf
));
}
}
sds
sdscatrepr
(
sds
s
,
char
*
p
,
size_t
len
)
{
s
=
sdscatlen
(
s
,
"
\"
"
,
1
);
while
(
len
--
)
{
switch
(
*
p
)
{
case
'\\'
:
case
'"'
:
s
=
sdscatprintf
(
s
,
"
\\
%c"
,
*
p
);
break
;
case
'\n'
:
s
=
sdscatlen
(
s
,
"
\\
n"
,
1
);
break
;
case
'\r'
:
s
=
sdscatlen
(
s
,
"
\\
r"
,
1
);
break
;
case
'\t'
:
s
=
sdscatlen
(
s
,
"
\\
t"
,
1
);
break
;
case
'\a'
:
s
=
sdscatlen
(
s
,
"
\\
a"
,
1
);
break
;
case
'\b'
:
s
=
sdscatlen
(
s
,
"
\\
b"
,
1
);
break
;
default:
if
(
isprint
(
*
p
))
s
=
sdscatprintf
(
s
,
"%c"
,
*
p
);
else
s
=
sdscatprintf
(
s
,
"
\\
x%02x"
,(
unsigned
char
)
*
p
);
break
;
}
p
++
;
}
return
sdscatlen
(
s
,
"
\"
"
,
1
);
}
/* Split a line into arguments, where every argument can be in the
* following programming-language REPL-alike form:
*
* foo bar "newline are supported\n" and "\xff\x00otherstuff"
*
* The number of arguments is stored into *argc, and an array
* of sds is returned. The caller should sdsfree() all the returned
* strings and finally free() the array itself.
*
* Note that sdscatrepr() is able to convert back a string into
* a quoted string in the same format sdssplitargs() is able to parse.
*/
sds
*
sdssplitargs
(
char
*
line
,
int
*
argc
)
{
char
*
p
=
line
;
char
*
current
=
NULL
;
char
**
vector
=
NULL
;
*
argc
=
0
;
while
(
1
)
{
/* skip blanks */
while
(
*
p
&&
isspace
(
*
p
))
p
++
;
if
(
*
p
)
{
/* get a token */
int
inq
=
0
;
/* set to 1 if we are in "quotes" */
int
done
=
0
;
if
(
current
==
NULL
)
current
=
sdsempty
();
while
(
!
done
)
{
if
(
inq
)
{
if
(
*
p
==
'\\'
&&
*
(
p
+
1
))
{
char
c
;
p
++
;
switch
(
*
p
)
{
case
'n'
:
c
=
'\n'
;
break
;
case
'r'
:
c
=
'\r'
;
break
;
case
't'
:
c
=
'\t'
;
break
;
case
'b'
:
c
=
'\b'
;
break
;
case
'a'
:
c
=
'\a'
;
break
;
default:
c
=
*
p
;
break
;
}
current
=
sdscatlen
(
current
,
&
c
,
1
);
}
else
if
(
*
p
==
'"'
)
{
/* closing quote must be followed by a space */
if
(
*
(
p
+
1
)
&&
!
isspace
(
*
(
p
+
1
)))
goto
err
;
done
=
1
;
}
else
if
(
!*
p
)
{
/* unterminated quotes */
goto
err
;
}
else
{
current
=
sdscatlen
(
current
,
p
,
1
);
}
}
else
{
switch
(
*
p
)
{
case
' '
:
case
'\n'
:
case
'\r'
:
case
'\t'
:
case
'\0'
:
done
=
1
;
break
;
case
'"'
:
inq
=
1
;
break
;
default:
current
=
sdscatlen
(
current
,
p
,
1
);
break
;
}
}
if
(
*
p
)
p
++
;
}
/* add the token to the vector */
vector
=
realloc
(
vector
,((
*
argc
)
+
1
)
*
sizeof
(
char
*
));
vector
[
*
argc
]
=
current
;
(
*
argc
)
++
;
current
=
NULL
;
}
else
{
return
vector
;
}
}
err:
while
((
*
argc
)
--
)
sdsfree
(
vector
[
*
argc
]);
free
(
vector
);
if
(
current
)
sdsfree
(
current
);
return
NULL
;
}
sds.h
View file @
3253105d
...
@@ -32,12 +32,13 @@
...
@@ -32,12 +32,13 @@
#define __SDS_H
#define __SDS_H
#include <sys/types.h>
#include <sys/types.h>
#include <stdarg.h>
typedef
char
*
sds
;
typedef
char
*
sds
;
struct
sdshdr
{
struct
sdshdr
{
long
len
;
int
len
;
long
free
;
int
free
;
char
buf
[];
char
buf
[];
};
};
...
@@ -53,6 +54,7 @@ sds sdscat(sds s, const char *t);
...
@@ -53,6 +54,7 @@ sds sdscat(sds s, const char *t);
sds
sdscpylen
(
sds
s
,
char
*
t
,
size_t
len
);
sds
sdscpylen
(
sds
s
,
char
*
t
,
size_t
len
);
sds
sdscpy
(
sds
s
,
char
*
t
);
sds
sdscpy
(
sds
s
,
char
*
t
);
sds
sdscatvprintf
(
sds
s
,
const
char
*
fmt
,
va_list
ap
);
#ifdef __GNUC__
#ifdef __GNUC__
sds
sdscatprintf
(
sds
s
,
const
char
*
fmt
,
...)
sds
sdscatprintf
(
sds
s
,
const
char
*
fmt
,
...)
__attribute__
((
format
(
printf
,
2
,
3
)));
__attribute__
((
format
(
printf
,
2
,
3
)));
...
@@ -61,7 +63,7 @@ sds sdscatprintf(sds s, const char *fmt, ...);
...
@@ -61,7 +63,7 @@ sds sdscatprintf(sds s, const char *fmt, ...);
#endif
#endif
sds
sdstrim
(
sds
s
,
const
char
*
cset
);
sds
sdstrim
(
sds
s
,
const
char
*
cset
);
sds
sdsrange
(
sds
s
,
long
start
,
long
end
);
sds
sdsrange
(
sds
s
,
int
start
,
int
end
);
void
sdsupdatelen
(
sds
s
);
void
sdsupdatelen
(
sds
s
);
int
sdscmp
(
sds
s1
,
sds
s2
);
int
sdscmp
(
sds
s1
,
sds
s2
);
sds
*
sdssplitlen
(
char
*
s
,
int
len
,
char
*
sep
,
int
seplen
,
int
*
count
);
sds
*
sdssplitlen
(
char
*
s
,
int
len
,
char
*
sep
,
int
seplen
,
int
*
count
);
...
@@ -69,5 +71,7 @@ void sdsfreesplitres(sds *tokens, int count);
...
@@ -69,5 +71,7 @@ void sdsfreesplitres(sds *tokens, int count);
void
sdstolower
(
sds
s
);
void
sdstolower
(
sds
s
);
void
sdstoupper
(
sds
s
);
void
sdstoupper
(
sds
s
);
sds
sdsfromlonglong
(
long
long
value
);
sds
sdsfromlonglong
(
long
long
value
);
sds
sdscatrepr
(
sds
s
,
char
*
p
,
size_t
len
);
sds
*
sdssplitargs
(
char
*
line
,
int
*
argc
);
#endif
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment