Skip to content

Commit 9ac2d76

Browse files
Kelvin LauKelvin Lau
authored andcommitted
Fixed up indentation and removed a few self references.
1 parent ebae302 commit 9ac2d76

File tree

1 file changed

+57
-57
lines changed

1 file changed

+57
-57
lines changed

Boyer-Moore/BoyerMoore.playground/Contents.swift

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
11
//: Playground - noun: a place where people can play
22

33
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 {
2647

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 }
4050

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+
}
6361
}
62+
return nil
63+
}
6464
}
6565

6666
// A few simple tests

0 commit comments

Comments
 (0)