Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
d4e07f17
Commit
d4e07f17
authored
Mar 10, 2011
by
Pieter Noordhuis
Browse files
Add new string to long long function
parent
cc4c964b
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/redis.h
View file @
d4e07f17
...
@@ -894,6 +894,7 @@ int stringmatchlen(const char *pattern, int patternLen,
...
@@ -894,6 +894,7 @@ int stringmatchlen(const char *pattern, int patternLen,
int
stringmatch
(
const
char
*
pattern
,
const
char
*
string
,
int
nocase
);
int
stringmatch
(
const
char
*
pattern
,
const
char
*
string
,
int
nocase
);
long
long
memtoll
(
const
char
*
p
,
int
*
err
);
long
long
memtoll
(
const
char
*
p
,
int
*
err
);
int
ll2string
(
char
*
s
,
size_t
len
,
long
long
value
);
int
ll2string
(
char
*
s
,
size_t
len
,
long
long
value
);
int
string2ll
(
char
*
s
,
size_t
len
,
long
long
*
value
);
int
d2string
(
char
*
s
,
size_t
len
,
double
value
);
int
d2string
(
char
*
s
,
size_t
len
,
double
value
);
int
isStringRepresentableAsLong
(
sds
s
,
long
*
longval
);
int
isStringRepresentableAsLong
(
sds
s
,
long
*
longval
);
int
isStringRepresentableAsLongLong
(
sds
s
,
long
long
*
longval
);
int
isStringRepresentableAsLongLong
(
sds
s
,
long
long
*
longval
);
...
...
src/util.c
View file @
d4e07f17
...
@@ -201,6 +201,63 @@ int ll2string(char *s, size_t len, long long value) {
...
@@ -201,6 +201,63 @@ int ll2string(char *s, size_t len, long long value) {
return
l
;
return
l
;
}
}
/* Convert a string into a long long. Returns 1 if the string could be parsed
* into a (non-overflowing) long long, 0 otherwise. The value will be set to
* the parsed value when appropriate. */
int
string2ll
(
char
*
s
,
size_t
slen
,
long
long
*
value
)
{
char
*
p
=
s
;
size_t
plen
=
0
;
int
negative
=
0
;
unsigned
long
long
v
;
if
(
plen
==
slen
)
return
0
;
if
(
p
[
0
]
==
'-'
)
{
negative
=
1
;
p
++
;
plen
++
;
/* Abort on only a negative sign. */
if
(
plen
==
slen
)
return
0
;
}
/* First digit should be 1-9. */
if
(
p
[
0
]
>=
'1'
&&
p
[
0
]
<=
'9'
)
{
v
=
p
[
0
]
-
'0'
;
p
++
;
plen
++
;
}
else
{
return
0
;
}
while
(
plen
<
slen
&&
p
[
0
]
>=
'0'
&&
p
[
0
]
<=
'9'
)
{
if
(
v
>
(
ULLONG_MAX
/
10
))
/* Overflow. */
return
0
;
v
*=
10
;
if
(
v
>
(
ULLONG_MAX
-
(
p
[
0
]
-
'0'
)))
/* Overflow. */
return
0
;
v
+=
p
[
0
]
-
'0'
;
p
++
;
plen
++
;
}
/* Return if not all bytes were used. */
if
(
plen
<
slen
)
return
0
;
if
(
negative
)
{
if
(
v
>
(
-
(
unsigned
long
long
)
LLONG_MIN
))
/* Overflow. */
return
0
;
if
(
value
!=
NULL
)
*
value
=
-
v
;
}
else
{
if
(
v
>
LLONG_MAX
)
/* Overflow. */
return
0
;
if
(
value
!=
NULL
)
*
value
=
v
;
}
return
1
;
}
/* Convert a double to a string representation. Returns the number of bytes
/* Convert a double to a string representation. Returns the number of bytes
* required. The representation should always be parsable by stdtod(3). */
* required. The representation should always be parsable by stdtod(3). */
int
d2string
(
char
*
buf
,
size_t
len
,
double
value
)
{
int
d2string
(
char
*
buf
,
size_t
len
,
double
value
)
{
...
...
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