|
6 | 6 | http://www.drdobbs.com/database/faster-string-searches/184408171
|
7 | 7 | */
|
8 | 8 | extension String {
|
9 |
| - func indexOf(pattern: String) -> String.Index? { |
10 |
| - // Cache the length of the search pattern because we're going to |
11 |
| - // use it a few times and it's expensive to calculate. |
12 |
| - let patternLength = pattern.characters.count |
13 |
| - assert(patternLength > 0) |
14 |
| - assert(patternLength <= self.characters.count) |
15 |
| - |
16 |
| - // Make the skip table. This table determines how far we skip ahead |
17 |
| - // when a character from the pattern is found. |
18 |
| - var skipTable = [Character: Int]() |
19 |
| - for (i, c) in pattern.characters.enumerate() { |
20 |
| - skipTable[c] = patternLength - i - 1 |
21 |
| - } |
22 |
| - |
23 |
| - // This points at the last character in the pattern. |
24 |
| - let p = pattern.endIndex.predecessor() |
25 |
| - let lastChar = pattern[p] |
26 |
| - |
27 |
| - // The pattern is scanned right-to-left, so skip ahead in the string by |
28 |
| - // the length of the pattern. (Minus 1 because startIndex already points |
29 |
| - // at the first character in the source string.) |
30 |
| - var i = self.startIndex.advancedBy(patternLength - 1) |
31 |
| - |
32 |
| - // This is a helper function that steps backwards through both strings |
33 |
| - // until we find a character that doesn’t match, or until we’ve reached |
34 |
| - // the beginning of the pattern. |
35 |
| - func backwards() -> String.Index? { |
36 |
| - var q = p |
37 |
| - var j = i |
38 |
| - while q > pattern.startIndex { |
39 |
| - j = j.predecessor() |
40 |
| - q = q.predecessor() |
41 |
| - if self[j] != pattern[q] { return nil } |
42 |
| - } |
43 |
| - return j |
44 |
| - } |
45 |
| - |
46 |
| - // The main loop. Keep going until the end of the string is reached. |
47 |
| - while i < self.endIndex { |
48 |
| - let c = self[i] |
49 |
| - |
50 |
| - // Does the current character match the last character from the pattern? |
51 |
| - if c == lastChar { |
52 |
| - |
53 |
| - // There is a possible match. Do a brute-force search backwards. |
54 |
| - if let k = backwards() { return k } |
55 |
| - |
56 |
| - // If no match, we can only safely skip one character ahead. |
57 |
| - i = i.successor() |
58 |
| - } else { |
59 |
| - // The characters are not equal, so skip ahead. The amount to skip is |
60 |
| - // determined by the skip table. If the character is not present in the |
61 |
| - // pattern, we can skip ahead by the full pattern length. However, if |
62 |
| - // the character *is* present in the pattern, there may be a match up |
63 |
| - // ahead and we can't skip as far. |
64 |
| - i = i.advancedBy(skipTable[c] ?? patternLength) |
65 |
| - } |
| 9 | + func indexOf(pattern: String) -> String.Index? { |
| 10 | + // Cache the length of the search pattern because we're going to |
| 11 | + // use it a few times and it's expensive to calculate. |
| 12 | + let patternLength = pattern.characters.count |
| 13 | + assert(patternLength > 0) |
| 14 | + assert(patternLength <= self.characters.count) |
| 15 | + |
| 16 | + // Make the skip table. This table determines how far we skip ahead |
| 17 | + // when a character from the pattern is found. |
| 18 | + var skipTable = [Character: Int]() |
| 19 | + for (i, c) in pattern.characters.enumerated() { |
| 20 | + skipTable[c] = patternLength - i - 1 |
| 21 | + } |
| 22 | + |
| 23 | + // This points at the last character in the pattern. |
| 24 | + let p = pattern.index(before: pattern.endIndex) |
| 25 | + let lastChar = pattern[p] |
| 26 | + |
| 27 | + // The pattern is scanned right-to-left, so skip ahead in the string by |
| 28 | + // the length of the pattern. (Minus 1 because startIndex already points |
| 29 | + // at the first character in the source string.) |
| 30 | + var i = self.index(startIndex, offsetBy: patternLength - 1) |
| 31 | + |
| 32 | + // This is a helper function that steps backwards through both strings |
| 33 | + // until we find a character that doesn’t match, or until we’ve reached |
| 34 | + // the beginning of the pattern. |
| 35 | + func backwards() -> String.Index? { |
| 36 | + var q = p |
| 37 | + var j = i |
| 38 | + while q > pattern.startIndex { |
| 39 | + j = index(before: j) |
| 40 | + q = index(before: q) |
| 41 | + if self[j] != pattern[q] { return nil } |
| 42 | + } |
| 43 | + return j |
| 44 | + } |
| 45 | + |
| 46 | + // The main loop. Keep going until the end of the string is reached. |
| 47 | + while i < self.endIndex { |
| 48 | + let c = self[i] |
| 49 | + |
| 50 | + // Does the current character match the last character from the pattern? |
| 51 | + if c == lastChar { |
| 52 | + |
| 53 | + // There is a possible match. Do a brute-force search backwards. |
| 54 | + if let k = backwards() { return k } |
| 55 | + |
| 56 | + // If no match, we can only safely skip one character ahead. |
| 57 | + i = index(after: i) |
| 58 | + } else { |
| 59 | + // The characters are not equal, so skip ahead. The amount to skip is |
| 60 | + // determined by the skip table. If the character is not present in the |
| 61 | + // pattern, we can skip ahead by the full pattern length. However, if |
| 62 | + // the character *is* present in the pattern, there may be a match up |
| 63 | + // ahead and we can't skip as far. |
| 64 | + i = self.index(i, offsetBy: skipTable[c] ?? patternLength) |
| 65 | + } |
| 66 | + } |
| 67 | + return nil |
66 | 68 | }
|
67 |
| - return nil |
68 |
| - } |
69 | 69 | }
|
0 commit comments