aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libidn/patches/07-binary-search.patch
blob: ad5583c45dc66d459fe805a7bd7cea6d32109828 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
--- contrib/libs/libidn/lib/stringprep.c	(index)
+++ contrib/libs/libidn/lib/stringprep.c	(working tree)
@@ -86,18 +86,40 @@ stringprep_find_character_in_table (uint32_t ucs4,
 }
 
 static ssize_t
+stringprep_find_character_in_table_fast (uint32_t ucs4,
+				    const Stringprep_table_element * table, size_t table_size)
+{
+  int l = 0;
+  int r = table_size - 1;
+  int pivot = (l + r) / 2;
+
+  while (l <= r) {
+    if (ucs4 >= table[pivot].start &&
+      ucs4 <= (table[pivot].end ? table[pivot].end : table[pivot].start))
+        return pivot;
+    else if (ucs4 < table[pivot].start)
+      r = pivot - 1;
+    else
+      l = pivot + 1;
+    pivot = (l + r) / 2;
+  }
+  if (l > r)
+    return -1;
+}
+
+static ssize_t
 stringprep_find_string_in_table (uint32_t *ucs4,
 				 size_t ucs4len,
 				 size_t *tablepos,
 				 const Stringprep_table_element *table,
-				 size_t table_size)
+				 const size_t table_size)
 {
   size_t j;
   ssize_t pos;
 
   for (j = 0; j < ucs4len; j++)
     if ((pos =
-	 stringprep_find_character_in_table (ucs4[j], table,
+	 stringprep_find_character_in_table_fast (ucs4[j], table,
 					     table_size)) != -1)
       {
 	if (tablepos)
@@ -113,7 +135,7 @@ stringprep_apply_table_to_string (uint32_t *ucs4,
 				  size_t *ucs4len,
 				  size_t maxucs4len,
 				  const Stringprep_table_element *table,
-				  size_t table_size)
+				  const size_t table_size)
 {
   ssize_t pos;
   size_t i, maplen;
@@ -302,16 +324,12 @@ stringprep_4i (uint32_t *ucs4, size_t *len, size_t maxucs4len,
 
 	    if (contains_ral != SIZE_MAX)
 	      {
-		if (!(stringprep_find_character_in_table
+		if (!(stringprep_find_character_in_table_fast
 		      (ucs4[0], profile[contains_ral].table,
-		       profile[contains_ral].table_size) != -1
-		      &&
-		      stringprep_find_character_in_table (ucs4[ucs4len - 1],
-							  profile
-							  [contains_ral].table,
-							  profile
-							  [contains_ral].table_size)
-		      != -1))
+		       profile[contains_ral].table_size) != -1 &&
+		      stringprep_find_character_in_table_fast
+		      (ucs4[ucs4len - 1], profile[contains_ral].table,
+		       profile[contains_ral].table_size) != -1))
 		  return STRINGPREP_BIDI_LEADTRAIL_NOT_RAL;
 	      }
 	  }