Skip to contentSkip to conversation

textsearch: fix Boyer-Moore algorithm for case insensitive searching

PATCH In review Joonwoo Park 3 participants 6 messages Jun 21, 2008 – Jun 23, 2008

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 3/8 textsearch: fix Boyer-Moore algorithm for case insensitive searching

Fix Boyer-Moore algorithm to have capability of case insensitive searching

---
 lib/ts_bm.c |   26 ++++++++++++++++++++------
 1 files changed, 20 insertions(+), 6 deletions(-)
lib/ts_bm.c +20 -6
index 4a7fce7..77749f3 100644
@@ -39,6 +39,7 @@
 #include <linux/module.h>
 #include <linux/types.h>
 #include <linux/string.h>
+#include <linux/ctype.h>
 #include <linux/textsearch.h>
 
 /* Alphabet size, use ASCII */
@@ -64,6 +65,7 @@ static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
 	unsigned int i, text_len, consumed = state->offset;
 	const u8 *text;
 	int shift = bm->patlen - 1, bs;
+	const u8 icase = conf->icase;
 
 	for (;;) {
 		text_len = conf->get_next_block(consumed, &text, conf, state);
@@ -75,7 +77,9 @@ static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
 			DEBUGP("Searching in position %d (%c)\n", 
 				shift, text[shift]);
 			for (i = 0; i < bm->patlen; i++) 
-			     if (text[shift-i] != bm->pattern[bm->patlen-1-i])
+				if ((icase ? toupper(text[shift-i])
+				    : text[shift-i])
+					!= bm->pattern[bm->patlen-1-i])
 				     goto next;
 
 			/* London calling... */
@@ -111,14 +115,18 @@ static int subpattern(u8 *pattern, int i, int j, int g)
 	return ret;
 }
 
-static void compute_prefix_tbl(struct ts_bm *bm)
+static void compute_prefix_tbl(struct ts_bm *bm, u8 icase)
 {
 	int i, j, g;
 
 	for (i = 0; i < ASIZE; i++)
 		bm->bad_shift[i] = bm->patlen;
-	for (i = 0; i < bm->patlen - 1; i++)
+	for (i = 0; i < bm->patlen - 1; i++) {
 		bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i;
+		if (icase)
+			bm->bad_shift[tolower(bm->pattern[i])]
+			    = bm->patlen - 1 - i;
+	}
 
 	/* Compute the good shift array, used to match reocurrences 
 	 * of a subpattern */
@@ -135,10 +143,11 @@ static void compute_prefix_tbl(struct ts_bm *bm)
 }
 
 static struct ts_config *bm_init(const void *pattern, unsigned int len,
-				 gfp_t gfp_mask)
+				 u8 icase, gfp_t gfp_mask)
 {
 	struct ts_config *conf;
 	struct ts_bm *bm;
+	int i;
 	unsigned int prefix_tbl_len = len * sizeof(unsigned int);
 	size_t priv_size = sizeof(*bm) + len + prefix_tbl_len;
 
@@ -146,11 +155,16 @@ static struct ts_config *bm_init(const void *pattern, unsigned int len,
 	if (IS_ERR(conf))
 		return conf;
 
+	conf->icase = icase;
 	bm = ts_config_priv(conf);
 	bm->patlen = len;
 	bm->pattern = (u8 *) bm->good_shift + prefix_tbl_len;
-	memcpy(bm->pattern, pattern, len);
-	compute_prefix_tbl(bm);
+	if (icase)
+		for (i = 0; i < len; i++)
+			bm->pattern[i] = toupper(((u8 *)pattern)[i]);
+	else
+		memcpy(bm->pattern, pattern, len);
+	compute_prefix_tbl(bm, icase);
 
 	return conf;
 }
Signature · 1 line
1.5.4.3
#1 lore raw
Quoted 19 lines · Joonwoo Park
lib/ts_bm.c +4 -2
@@ -75,7 +77,9 @@ static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
 			DEBUGP("Searching in position %d (%c)\n", 
 				shift, text[shift]);
 			for (i = 0; i < bm->patlen; i++) 
-			     if (text[shift-i] != bm->pattern[bm->patlen-1-i])
+				if ((icase ? toupper(text[shift-i])
+				    : text[shift-i])
+					!= bm->pattern[bm->patlen-1-i])
 				     goto next;
 
 			/* London calling... */
@@ -111,14 +115,18 @@ static int subpattern(u8 *pattern, int i, int j, int g)
 	return ret;
 }
 
-static void compute_prefix_tbl(struct ts_bm *bm)
+static void compute_prefix_tbl(struct ts_bm *bm, u8 icase)
 {
 	int i, j, g;

lib/ts_bm.c static int subpattern(u8 *pattern, int i, int j, int g) · line 129

Quoted 10 lines
lib/ts_bm.c +5 -1
 
 	for (i = 0; i < ASIZE; i++)
 		bm->bad_shift[i] = bm->patlen;
-	for (i = 0; i < bm->patlen - 1; i++)
+	for (i = 0; i < bm->patlen - 1; i++) {
 		bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i;
+		if (icase)
+			bm->bad_shift[tolower(bm->pattern[i])]
+			    = bm->patlen - 1 - i;
+	}

You use toupper() above and tolower() here, is that correct?

#2 lore raw

2008/6/21 Patrick McHardy <[email protected]>:

Quoted 24 lines
Quoted 23 lines · Joonwoo Park
lib/ts_bm.c +4 -1
@@ -75,7 +77,9 @@ static unsigned int bm_find(struct ts_config *conf,
 struct ts_state *state)
                       DEBUGP("Searching in position %d (%c)\n",
                     shift, text[shift]);
                       for (i = 0; i < bm->patlen; i++) -
           if (text[shift-i] != bm->pattern[bm->patlen-1-i])
+                               if ((icase ? toupper(text[shift-i])
+                                   : text[shift-i])
+                                       != bm->pattern[bm->patlen-1-i])
                                    goto next;
                         /* London calling... */
@@ -111,14 +115,18 @@ static int subpattern(u8 *pattern, int i, int j, int
 g)
       return ret;
 }
 -static void compute_prefix_tbl(struct ts_bm *bm)
+static void compute_prefix_tbl(struct ts_bm *bm, u8 icase)
 {
       int i, j, g;
         for (i = 0; i < ASIZE; i++)
               bm->bad_shift[i] = bm->patlen;
-       for (i = 0; i < bm->patlen - 1; i++)
Quoted 9 lines

lib/ts_bm.c static int subpattern(u8 *pattern, int i, int j, int g) · line 129

Quoted 6 lines
lib/ts_bm.c +5 -0
+       for (i = 0; i < bm->patlen - 1; i++) {
               bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i;
+               if (icase)
+                       bm->bad_shift[tolower(bm->pattern[i])]
+                           = bm->patlen - 1 - i;
+       }

You use toupper() above and tolower() here, is that correct?

It should be, bm->pattern's characters are all upper case since It was altered before.
So we should use toupper() for target string character to compare. And to ignore case, we should prepare one more bad_shift array calculation, opposite tolower() should be used for it.

#3 lore raw
Quoted 13 lines · Joonwoo Park

2008/6/21 Patrick McHardy <[email protected]>:

Quoted 12 lines
Quoted 12 lines
lib/ts_bm.c +6 -1
+static void compute_prefix_tbl(struct ts_bm *bm, u8 icase)
 {
       int i, j, g;
         for (i = 0; i < ASIZE; i++)
               bm->bad_shift[i] = bm->patlen;
-       for (i = 0; i < bm->patlen - 1; i++)
+       for (i = 0; i < bm->patlen - 1; i++) {
               bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i;
+               if (icase)
+                       bm->bad_shift[tolower(bm->pattern[i])]
+                           = bm->patlen - 1 - i;
+       }
Quoted 8 lines
Quoted 2 lines

You use toupper() above and tolower() here, is that correct?

It should be, bm->pattern's characters are all upper case since It was altered before.
So we should use toupper() for target string character to compare. And to ignore case, we should prepare one more bad_shift array calculation, opposite tolower() should be used for it.

OK thanks. I'll wait for an ACK from Thomas before applying them though.

#4 lore raw
Quoted 17 lines · Patrick McHardy
Quoted 16 lines · Joonwoo Park

2008/6/21 Patrick McHardy <[email protected]>:

Quoted 14 lines

lib/ts_bm.c static int subpattern(u8 *pattern, int i, int j, int g) · line 129

Quoted 12 lines
lib/ts_bm.c +6 -1
+static void compute_prefix_tbl(struct ts_bm *bm, u8 icase)
 {
       int i, j, g;
         for (i = 0; i < ASIZE; i++)
               bm->bad_shift[i] = bm->patlen;
-       for (i = 0; i < bm->patlen - 1; i++)
+       for (i = 0; i < bm->patlen - 1; i++) {
               bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i;
+               if (icase)
+                       bm->bad_shift[tolower(bm->pattern[i])]
+                           = bm->patlen - 1 - i;
+       }

You use toupper() above and tolower() here, is that correct?

Quoted 8 lines
Quoted 5 lines

It should be, bm->pattern's characters are all upper case since It was altered before.
So we should use toupper() for target string character to compare. And to ignore case, we should prepare one more bad_shift array calculation, opposite tolower() should be used for it.

OK thanks. I'll wait for an ACK from Thomas before applying them though.

This is rather confusing. Why not set both the pattern and the bad_shift to lower or upper case?

Signature · 1 line
"Los honestos son inadaptados sociales" -- Les Luthiers
#5 lore raw

2008/6/23 Pablo Neira Ayuso <[email protected]>:

Quoted 24 lines
Quoted 23 lines · Patrick McHardy
Quoted 21 lines · Joonwoo Park

2008/6/21 Patrick McHardy <[email protected]>:

Quoted 12 lines

lib/ts_bm.c static int subpattern(u8 *pattern, int i, int j, int g) · line 129

Quoted 12 lines
lib/ts_bm.c +6 -1
+static void compute_prefix_tbl(struct ts_bm *bm, u8 icase)
 {
       int i, j, g;
         for (i = 0; i < ASIZE; i++)
               bm->bad_shift[i] = bm->patlen;
-       for (i = 0; i < bm->patlen - 1; i++)
+       for (i = 0; i < bm->patlen - 1; i++) {
               bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i;
+               if (icase)
+                       bm->bad_shift[tolower(bm->pattern[i])]
+                           = bm->patlen - 1 - i;
+       }
Quoted 2 lines

You use toupper() above and tolower() here, is that correct?

It should be, bm->pattern's characters are all upper case since It was altered before.
So we should use toupper() for target string character to compare. And to ignore case, we should prepare one more bad_shift array calculation, opposite tolower() should be used for it.

Quoted 6 lines
Quoted 2 lines

OK thanks. I'll wait for an ACK from Thomas before applying them though.

This is rather confusing. Why not set both the pattern and the bad_shift to lower or upper case?

To be honest, pattern can be set to lower or upper case, it doesn't matter. But for bad_shift, we should add one more array for diffrence of case. So bad_shift array should have both of lower and upper.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
+       for (i = 0; i < bm->patlen - 1; i++) {
               bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i;

Therefore, at here, this makes bad_shift for upper case,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

lib/ts_bm.c static int subpattern(u8 *pattern, int i, int j, int g) · line 128

Quoted 3 lines
lib/ts_bm.c +3 -0
+               if (icase)
+                       bm->bad_shift[tolower(bm->pattern[i])]
+                           = bm->patlen - 1 - i;

At here, this makes bad_shift for lower case.