Skip to content

Commit 3d26e92

Browse files
author
Kevin Taniguchi
committed
updated readme
1 parent df1cc1e commit 3d26e92

File tree

3 files changed

+47
-43
lines changed

3 files changed

+47
-43
lines changed

Boyer-Moore/BoyerMoore.playground/Contents.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ s.indexOf(pattern: "World") // 7
4646

4747
let animals = "🐶🐔🐷🐮🐱"
4848
animals.indexOf(pattern: "🐮") // 6
49+
50+
51+

Boyer-Moore/BoyerMoore.playground/timeline.xctimeline

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
version = "3.0">
44
<TimelineItems>
55
<LoggerValueHistoryTimelineItem
6-
documentLocation = "#CharacterRangeLen=1&amp;CharacterRangeLoc=345&amp;EndingColumnNumber=37&amp;EndingLineNumber=9&amp;StartingColumnNumber=9&amp;StartingLineNumber=9&amp;Timestamp=497331454.819397"
6+
documentLocation = "#CharacterRangeLen=1&amp;CharacterRangeLoc=345&amp;EndingColumnNumber=37&amp;EndingLineNumber=9&amp;StartingColumnNumber=9&amp;StartingLineNumber=9&amp;Timestamp=497453081.589625"
77
selectedRepresentationIndex = "0"
88
shouldTrackSuperviewWidth = "NO">
99
</LoggerValueHistoryTimelineItem>
1010
<LoggerValueHistoryTimelineItem
11-
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=345&amp;EndingColumnNumber=26&amp;EndingLineNumber=9&amp;StartingColumnNumber=9&amp;StartingLineNumber=9&amp;Timestamp=497331454.819493"
11+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=345&amp;EndingColumnNumber=26&amp;EndingLineNumber=9&amp;StartingColumnNumber=9&amp;StartingLineNumber=9&amp;Timestamp=497453081.589795"
1212
selectedRepresentationIndex = "0"
1313
shouldTrackSuperviewWidth = "NO">
1414
</LoggerValueHistoryTimelineItem>
1515
<LoggerValueHistoryTimelineItem
16-
documentLocation = "#CharacterRangeLen=1&amp;CharacterRangeLoc=345&amp;EndingColumnNumber=25&amp;EndingLineNumber=9&amp;StartingColumnNumber=9&amp;StartingLineNumber=9&amp;Timestamp=497331454.819555"
16+
documentLocation = "#CharacterRangeLen=1&amp;CharacterRangeLoc=345&amp;EndingColumnNumber=25&amp;EndingLineNumber=9&amp;StartingColumnNumber=9&amp;StartingLineNumber=9&amp;Timestamp=497453081.589932"
1717
selectedRepresentationIndex = "0"
1818
shouldTrackSuperviewWidth = "NO">
1919
</LoggerValueHistoryTimelineItem>

Boyer-Moore/README.markdown

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ For example:
99
```swift
1010
// Input:
1111
let s = "Hello, World"
12-
s.indexOf("World")
12+
s.indexOf(pattern: "World")
1313

1414
// Output:
1515
<String.Index?> 7
1616

1717
// Input:
1818
let animals = "🐶🐔🐷🐮🐱"
19-
animals.indexOf("🐮")
19+
animals.indexOf(pattern: "🐮")
2020

2121
// Output:
2222
<String.Index?> 6
@@ -32,67 +32,68 @@ Here's how you could write it in Swift:
3232

3333
```swift
3434
extension String {
35-
func indexOf(pattern: String) -> String.Index? {
36-
// Cache the length of the search pattern because we're going to
37-
// use it a few times and it's expensive to calculate.
35+
func indexOf(pattern: String) -> String.Index? {
36+
// Cache the length of the search pattern because we're going to
37+
// use it a few times and it's expensive to calculate.
3838
let patternLength = pattern.characters.count
39-
assert(patternLength > 0)
40-
assert(patternLength <= self.characters.count)
39+
assert(patternLength > 0)
40+
assert(patternLength <= self.characters.count)
4141

42-
// Make the skip table. This table determines how far we skip ahead
43-
// when a character from the pattern is found.
42+
// Make the skip table. This table determines how far we skip ahead
43+
// when a character from the pattern is found.
4444
var skipTable = [Character: Int]()
4545
for (i, c) in pattern.characters.enumerate() {
46-
skipTable[c] = patternLength - i - 1
46+
skipTable[c] = patternLength - i - 1
4747
}
4848

49-
// This points at the last character in the pattern.
49+
// This points at the last character in the pattern.
5050
let p = pattern.endIndex.predecessor()
5151
let lastChar = pattern[p]
5252

53-
// The pattern is scanned right-to-left, so skip ahead in the string by
54-
// the length of the pattern. (Minus 1 because startIndex already points
55-
// at the first character in the source string.)
53+
// The pattern is scanned right-to-left, so skip ahead in the string by
54+
// the length of the pattern. (Minus 1 because startIndex already points
55+
// at the first character in the source string.)
5656
var i = self.startIndex.advancedBy(patternLength - 1)
5757

58-
// This is a helper function that steps backwards through both strings
59-
// until we find a character that doesn’t match, or until we’ve reached
60-
// the beginning of the pattern.
58+
// This is a helper function that steps backwards through both strings
59+
// until we find a character that doesn’t match, or until we’ve reached
60+
// the beginning of the pattern.
6161
func backwards() -> String.Index? {
62-
var q = p
63-
var j = i
64-
while q > pattern.startIndex {
65-
j = j.predecessor()
66-
q = q.predecessor()
67-
if self[j] != pattern[q] { return nil }
68-
}
69-
return j
62+
var q = p
63+
var j = i
64+
while q > pattern.startIndex {
65+
j = j.predecessor()
66+
q = q.predecessor()
67+
if self[j] != pattern[q] { return nil }
68+
}
69+
return j
7070
}
7171

72-
// The main loop. Keep going until the end of the string is reached.
72+
// The main loop. Keep going until the end of the string is reached.
7373
while i < self.endIndex {
74-
let c = self[i]
74+
let c = self[i]
7575

76-
// Does the current character match the last character from the pattern?
77-
if c == lastChar {
76+
// Does the current character match the last character from the pattern?
77+
if c == lastChar {
7878

79-
// There is a possible match. Do a brute-force search backwards.
79+
// There is a possible match. Do a brute-force search backwards.
8080
if let k = backwards() { return k }
8181

82-
// If no match, we can only safely skip one character ahead.
82+
// If no match, we can only safely skip one character ahead.
8383
i = i.successor()
84-
} else {
85-
// The characters are not equal, so skip ahead. The amount to skip is
86-
// determined by the skip table. If the character is not present in the
87-
// pattern, we can skip ahead by the full pattern length. However, if
88-
// the character *is* present in the pattern, there may be a match up
89-
// ahead and we can't skip as far.
84+
} else {
85+
// The characters are not equal, so skip ahead. The amount to skip is
86+
// determined by the skip table. If the character is not present in the
87+
// pattern, we can skip ahead by the full pattern length. However, if
88+
// the character *is* present in the pattern, there may be a match up
89+
// ahead and we can't skip as far.
9090
i = i.advancedBy(skipTable[c] ?? patternLength)
91-
}
91+
}
9292
}
93-
return nil
94-
}
93+
return nil
94+
}
9595
}
96+
9697
```
9798

9899
The algorithm works as follows. You line up the search pattern with the source string and see what character from the string matches the *last* character of the search pattern:

0 commit comments

Comments
 (0)