summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2020-04-01 17:36:32 +0200
committerantirez <antirez@gmail.com>2020-04-01 17:36:32 +0200
commit4cbf3f5dddc7c765269d8ce9eceb406ccde036d6 (patch)
tree883c1bc9bd24cc7a5afeeab7c2074ba2ece6eda3
parentc9c03c3ee60b6a7a4918d4b2bbf40cfd21fcd284 (diff)
downloadredis-4cbf3f5dddc7c765269d8ce9eceb406ccde036d6.tar.gz
LCS: other fixes to range emission.
-rw-r--r--src/t_string.c36
1 files changed, 20 insertions, 16 deletions
diff --git a/src/t_string.c b/src/t_string.c
index caccd11f1..30999ee70 100644
--- a/src/t_string.c
+++ b/src/t_string.c
@@ -584,12 +584,13 @@ void lcsCommand(client *c) {
i = alen, j = blen;
while (computelcs && i > 0 && j > 0) {
+ int emit_range = 0;
if (a[i-1] == b[j-1]) {
/* If there is a match, store the character and reduce
* the indexes to look for a new match. */
result[idx-1] = a[i-1];
+
/* Track the current range. */
- int emit_range = 0;
if (arange_start == alen) {
arange_start = i-1;
arange_end = i-1;
@@ -605,22 +606,9 @@ void lcsCommand(client *c) {
emit_range = 1;
}
}
+ /* Emit the range if we matched with the first byte of
+ * one of the two strings. We'll exit the loop ASAP. */
if (arange_start == 0 || brange_start == 0) emit_range = 1;
-
- /* Emit the current range if needed. */
- if (emit_range) {
- if (arraylenptr) {
- addReplyArrayLen(c,2);
- addReplyArrayLen(c,2);
- addReplyLongLong(c,arange_start);
- addReplyLongLong(c,arange_end);
- addReplyArrayLen(c,2);
- addReplyLongLong(c,brange_start);
- addReplyLongLong(c,brange_end);
- }
- arange_start = alen; /* Restart at the next match. */
- arraylen++;
- }
idx--; i--; j--;
} else {
/* Otherwise reduce i and j depending on the largest
@@ -631,6 +619,22 @@ void lcsCommand(client *c) {
i--;
else
j--;
+ if (arange_start != alen) emit_range = 1;
+ }
+
+ /* Emit the current range if needed. */
+ if (emit_range) {
+ if (arraylenptr) {
+ addReplyArrayLen(c,2);
+ addReplyArrayLen(c,2);
+ addReplyLongLong(c,arange_start);
+ addReplyLongLong(c,arange_end);
+ addReplyArrayLen(c,2);
+ addReplyLongLong(c,brange_start);
+ addReplyLongLong(c,brange_end);
+ }
+ arange_start = alen; /* Restart at the next match. */
+ arraylen++;
}
}