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
c9550788
Commit
c9550788
authored
Feb 27, 2015
by
antirez
Browse files
Utils: added function to get radix 10 string length of signed integer.
parent
7e6b4ea6
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/util.c
View file @
c9550788
...
@@ -251,6 +251,18 @@ uint32_t digits10(uint64_t v) {
...
@@ -251,6 +251,18 @@ uint32_t digits10(uint64_t v) {
return
12
+
digits10
(
v
/
1000000000000UL
);
return
12
+
digits10
(
v
/
1000000000000UL
);
}
}
/* Like digits10() but for signed values. */
uint32_t
sdigits10
(
int64_t
v
)
{
if
(
v
<
0
)
{
/* Abs value of LLONG_MIN requires special handling. */
uint64_t
uv
=
(
v
!=
LLONG_MIN
)
?
-
v
:
((
uint64_t
)
LLONG_MAX
)
+
1
;
return
digits10
(
uv
)
+
1
;
/* +1 for the minus. */
}
else
{
return
digits10
(
v
);
}
}
/* Convert a long long into a string. Returns the number of
/* Convert a long long into a string. Returns the number of
* characters needed to represent the number.
* characters needed to represent the number.
* If the buffer is not big enough to store the string, 0 is returned.
* If the buffer is not big enough to store the string, 0 is returned.
...
...
src/util.h
View file @
c9550788
...
@@ -35,6 +35,8 @@
...
@@ -35,6 +35,8 @@
int
stringmatchlen
(
const
char
*
p
,
int
plen
,
const
char
*
s
,
int
slen
,
int
nocase
);
int
stringmatchlen
(
const
char
*
p
,
int
plen
,
const
char
*
s
,
int
slen
,
int
nocase
);
int
stringmatch
(
const
char
*
p
,
const
char
*
s
,
int
nocase
);
int
stringmatch
(
const
char
*
p
,
const
char
*
s
,
int
nocase
);
long
long
memtoll
(
const
char
*
p
,
int
*
err
);
long
long
memtoll
(
const
char
*
p
,
int
*
err
);
uint32_t
digits10
(
uint64_t
v
);
uint32_t
sdigits10
(
int64_t
v
);
int
ll2string
(
char
*
s
,
size_t
len
,
long
long
value
);
int
ll2string
(
char
*
s
,
size_t
len
,
long
long
value
);
int
string2ll
(
const
char
*
s
,
size_t
slen
,
long
long
*
value
);
int
string2ll
(
const
char
*
s
,
size_t
slen
,
long
long
*
value
);
int
string2l
(
const
char
*
s
,
size_t
slen
,
long
*
value
);
int
string2l
(
const
char
*
s
,
size_t
slen
,
long
*
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