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
redis
Commits
2e6b9949
Commit
2e6b9949
authored
May 18, 2016
by
antirez
Browse files
Code to access object string bytes repeated 3x refactored into 1 function.
parent
dcaeafc8
Changes
1
Show whitespace changes
Inline
Side-by-side
src/bitops.c
View file @
2e6b9949
...
@@ -476,6 +476,37 @@ robj *lookupStringForBitCommand(client *c, size_t maxbit) {
...
@@ -476,6 +476,37 @@ robj *lookupStringForBitCommand(client *c, size_t maxbit) {
return
o
;
return
o
;
}
}
/* Return a pointer to the string object content, and stores its length
* in 'len'. The user is required to pass (likely stack allocated) buffer
* 'llbuf' of at least LONG_STR_SIZE bytes. Such a buffer is used in the case
* the object is integer encoded in order to provide the representation
* without usign heap allocation.
*
* The function returns the pointer to the object array of bytes representing
* the string it contains, that may be a pointer to 'llbuf' or to the
* internal object representation. As a side effect 'len' is filled with
* the length of such buffer.
*
* If the source object is NULL the function is guaranteed to return NULL
* and set 'len' to 0. */
unsigned
char
*
getObjectReadOnlyString
(
robj
*
o
,
long
*
len
,
char
*
llbuf
)
{
serverAssert
(
o
->
type
==
OBJ_STRING
);
unsigned
char
*
p
=
NULL
;
/* Set the 'p' pointer to the string, that can be just a stack allocated
* array if our string was integer encoded. */
if
(
o
&&
o
->
encoding
==
OBJ_ENCODING_INT
)
{
p
=
(
unsigned
char
*
)
llbuf
;
if
(
len
)
*
len
=
ll2string
(
llbuf
,
LONG_STR_SIZE
,(
long
)
o
->
ptr
);
}
else
if
(
o
)
{
p
=
(
unsigned
char
*
)
o
->
ptr
;
if
(
len
)
*
len
=
sdslen
(
o
->
ptr
);
}
else
{
if
(
len
)
*
len
=
0
;
}
return
p
;
}
/* SETBIT key offset bitvalue */
/* SETBIT key offset bitvalue */
void
setbitCommand
(
client
*
c
)
{
void
setbitCommand
(
client
*
c
)
{
robj
*
o
;
robj
*
o
;
...
@@ -721,21 +752,12 @@ void bitcountCommand(client *c) {
...
@@ -721,21 +752,12 @@ void bitcountCommand(client *c) {
robj
*
o
;
robj
*
o
;
long
start
,
end
,
strlen
;
long
start
,
end
,
strlen
;
unsigned
char
*
p
;
unsigned
char
*
p
;
char
llbuf
[
32
];
char
llbuf
[
LONG_STR_SIZE
];
/* Lookup, check for type, and return 0 for non existing keys. */
/* Lookup, check for type, and return 0 for non existing keys. */
if
((
o
=
lookupKeyReadOrReply
(
c
,
c
->
argv
[
1
],
shared
.
czero
))
==
NULL
||
if
((
o
=
lookupKeyReadOrReply
(
c
,
c
->
argv
[
1
],
shared
.
czero
))
==
NULL
||
checkType
(
c
,
o
,
OBJ_STRING
))
return
;
checkType
(
c
,
o
,
OBJ_STRING
))
return
;
p
=
getObjectReadOnlyString
(
o
,
&
strlen
,
llbuf
);
/* Set the 'p' pointer to the string, that can be just a stack allocated
* array if our string was integer encoded. */
if
(
o
->
encoding
==
OBJ_ENCODING_INT
)
{
p
=
(
unsigned
char
*
)
llbuf
;
strlen
=
ll2string
(
llbuf
,
sizeof
(
llbuf
),(
long
)
o
->
ptr
);
}
else
{
p
=
(
unsigned
char
*
)
o
->
ptr
;
strlen
=
sdslen
(
o
->
ptr
);
}
/* Parse start/end range if any. */
/* Parse start/end range if any. */
if
(
c
->
argc
==
4
)
{
if
(
c
->
argc
==
4
)
{
...
@@ -775,7 +797,7 @@ void bitposCommand(client *c) {
...
@@ -775,7 +797,7 @@ void bitposCommand(client *c) {
robj
*
o
;
robj
*
o
;
long
bit
,
start
,
end
,
strlen
;
long
bit
,
start
,
end
,
strlen
;
unsigned
char
*
p
;
unsigned
char
*
p
;
char
llbuf
[
32
];
char
llbuf
[
LONG_STR_SIZE
];
int
end_given
=
0
;
int
end_given
=
0
;
/* Parse the bit argument to understand what we are looking for, set
/* Parse the bit argument to understand what we are looking for, set
...
@@ -795,16 +817,7 @@ void bitposCommand(client *c) {
...
@@ -795,16 +817,7 @@ void bitposCommand(client *c) {
return
;
return
;
}
}
if
(
checkType
(
c
,
o
,
OBJ_STRING
))
return
;
if
(
checkType
(
c
,
o
,
OBJ_STRING
))
return
;
p
=
getObjectReadOnlyString
(
o
,
&
strlen
,
llbuf
);
/* Set the 'p' pointer to the string, that can be just a stack allocated
* array if our string was integer encoded. */
if
(
o
->
encoding
==
OBJ_ENCODING_INT
)
{
p
=
(
unsigned
char
*
)
llbuf
;
strlen
=
ll2string
(
llbuf
,
sizeof
(
llbuf
),(
long
)
o
->
ptr
);
}
else
{
p
=
(
unsigned
char
*
)
o
->
ptr
;
strlen
=
sdslen
(
o
->
ptr
);
}
/* Parse start/end range if any. */
/* Parse start/end range if any. */
if
(
c
->
argc
==
4
||
c
->
argc
==
5
)
{
if
(
c
->
argc
==
4
||
c
->
argc
==
5
)
{
...
@@ -1036,21 +1049,12 @@ void bitfieldCommand(client *c) {
...
@@ -1036,21 +1049,12 @@ void bitfieldCommand(client *c) {
}
else
{
}
else
{
/* GET */
/* GET */
unsigned
char
buf
[
9
];
unsigned
char
buf
[
9
];
size_t
olen
=
0
;
long
strlen
;
unsigned
char
*
src
=
NULL
;
unsigned
char
*
src
=
NULL
;
char
llbuf
[
32
];
char
llbuf
[
LONG_STR_SIZE
];
o
=
lookupKeyRead
(
c
->
db
,
c
->
argv
[
1
]);
o
=
lookupKeyRead
(
c
->
db
,
c
->
argv
[
1
]);
src
=
getObjectReadOnlyString
(
o
,
&
strlen
,
llbuf
);
/* Set the 'p' pointer to the string, that can be just a stack allocated
* array if our string was integer encoded. */
if
(
o
&&
o
->
encoding
==
OBJ_ENCODING_INT
)
{
src
=
(
unsigned
char
*
)
llbuf
;
olen
=
ll2string
(
llbuf
,
sizeof
(
llbuf
),(
long
)
o
->
ptr
);
}
else
if
(
o
)
{
src
=
(
unsigned
char
*
)
o
->
ptr
;
olen
=
sdslen
(
o
->
ptr
);
}
/* For GET we use a trick: before executing the operation
/* For GET we use a trick: before executing the operation
* copy up to 9 bytes to a local buffer, so that we can easily
* copy up to 9 bytes to a local buffer, so that we can easily
...
@@ -1060,7 +1064,7 @@ void bitfieldCommand(client *c) {
...
@@ -1060,7 +1064,7 @@ void bitfieldCommand(client *c) {
int
i
;
int
i
;
size_t
byte
=
thisop
->
offset
>>
3
;
size_t
byte
=
thisop
->
offset
>>
3
;
for
(
i
=
0
;
i
<
9
;
i
++
)
{
for
(
i
=
0
;
i
<
9
;
i
++
)
{
if
(
src
==
NULL
||
i
+
byte
>=
o
len
)
break
;
if
(
src
==
NULL
||
i
+
byte
>=
(
size_t
)
str
len
)
break
;
buf
[
i
]
=
src
[
i
+
byte
];
buf
[
i
]
=
src
[
i
+
byte
];
}
}
...
...
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