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