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
c09c2c3b
Commit
c09c2c3b
authored
May 22, 2010
by
Pieter Noordhuis
Browse files
code to compare strings with entries in ziplist, regardless of their encoding
parent
75d8978e
Changes
1
Hide whitespace changes
Inline
Side-by-side
ziplist.c
View file @
c09c2c3b
...
...
@@ -365,6 +365,28 @@ unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
return
zl
;
}
/* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
unsigned
int
ziplistCompare
(
unsigned
char
*
p
,
unsigned
char
*
entry
,
unsigned
int
elen
)
{
unsigned
int
zlen
,
lensize
;
char
encoding
;
long
long
zval
,
eval
;
if
(
*
p
==
ZIP_END
)
return
0
;
zlen
=
zipDecodeLength
(
p
,
&
lensize
);
if
(
zipTryEncoding
(
entry
,
&
eval
,
&
encoding
))
{
/* Do integer compare */
zval
=
zipLoadInteger
(
p
+
lensize
,
ZIP_ENCODING
(
p
));
return
zval
==
eval
;
}
else
{
/* Raw compare */
if
(
zlen
==
elen
)
{
return
memcmp
(
p
+
lensize
,
entry
,
elen
)
==
0
;
}
else
{
return
0
;
}
}
}
void
ziplistRepr
(
unsigned
char
*
zl
)
{
unsigned
char
*
p
,
encoding
;
unsigned
int
l
,
lsize
;
...
...
@@ -557,7 +579,31 @@ int main(int argc, char **argv) {
}
printf
(
"
\n
"
);
ziplistRepr
(
zl
);
printf
(
"
\n
"
);
}
printf
(
"Compare strings with ziplist entries:
\n
"
);
{
zl
=
createList
();
p
=
ziplistIndex
(
zl
,
0
);
if
(
!
ziplistCompare
(
p
,
"hello"
,
5
))
{
printf
(
"ERROR
\n
"
);
return
;
}
if
(
ziplistCompare
(
p
,
"hella"
,
5
))
{
printf
(
"ERROR
\n
"
);
return
;
}
p
=
ziplistIndex
(
zl
,
3
);
if
(
!
ziplistCompare
(
p
,
"1024"
,
4
))
{
printf
(
"ERROR
\n
"
);
return
;
}
if
(
ziplistCompare
(
p
,
"1025"
,
4
))
{
printf
(
"ERROR
\n
"
);
return
;
}
printf
(
"SUCCESS
\n
"
);
}
return
0
;
...
...
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