Skip to content

Commit c8bfaaf

Browse files
authored
Merge pull request #4 from john-mueller/updates
Testing and bundling updates Updates configuration files to use main branch and more recent Swift versions. Uses resource bundle for testing, and removes unneeded testing code. Closes #3 Removes uses of #file in default Hyphenator creation. Closes #2 Fixes out-of-bounds error in setting Hyphenator properties.
2 parents 057c88f + 697fbbb commit c8bfaaf

28 files changed

+130
-140
lines changed

.github/workflows/swiftlint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: SwiftLint
22
on:
33
push:
44
branches:
5-
- master
5+
- main
66
pull_request:
77
paths:
88
- '.github/workflows/swiftlint.yml'
@@ -14,6 +14,6 @@ jobs:
1414
steps:
1515
- uses: actions/checkout@v2
1616
- name: SwiftLint
17-
uses: norio-nomura/action-swiftlint@3.1.0
17+
uses: norio-nomura/action-swiftlint@3.2.1
1818
with:
1919
args: --strict

.github/workflows/tests.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ name: Tests
22
on:
33
push:
44
branches:
5-
- master
5+
- main
66
pull_request:
77
branches:
8-
- master
8+
- main
99
jobs:
1010
macOS:
1111
name: macOS
@@ -25,16 +25,16 @@ jobs:
2525
- uses: actions/checkout@v2
2626
- name: Setup
2727
run: |
28-
export API_URL="https://swiftenv-api.fuller.li/versions?snapshot=false&platform=ubuntu18.04"
28+
export API_URL="https://swiftenv-api.fuller.li/versions?snapshot=false&platform=ubuntu20.04"
2929
export SWIFT_VERSION="$(curl -H 'Accept: text/plain' "$API_URL" | tail -n1)"
3030
3131
git clone --depth 1 https://github.com/kylef/swiftenv.git ~/.swiftenv
3232
export SWIFTENV_ROOT="$HOME/.swiftenv"
3333
export PATH="$SWIFTENV_ROOT/bin:$SWIFTENV_ROOT/shims:$PATH"
3434
swiftenv install -s
3535
36-
echo "::set-env name=SWIFT_VERSION::$SWIFT_VERSION"
37-
echo "::add-path::$SWIFTENV_ROOT/shims"
36+
echo "SWIFT_VERSION=$SWIFT_VERSION" >> $GITHUB_ENV
37+
echo "$SWIFTENV_ROOT/shims" >> $GITHUB_PATH
3838
- name: Version
3939
run: |
4040
swift --version

.jazzy.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ swift_build_tool: spm
33
author: John Mueller
44
author_url: https://john-mueller.github.io/
55
module: Hyphenation
6-
module_version: v0.1.0
7-
copyright: © 2020 John Mueller
6+
module_version: v0.2.0
7+
copyright: © 2020-2021 John Mueller
88
github_url: https://github.com/john-mueller/Hyphenation/
99
min_acl: public
1010
theme: fullwidth

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ test: test-correctness test-thread-safety
77
bench: test-performance
88

99
test-correctness:
10-
HYPHENATION_TEST_TYPE=hyphenation swift test -c debug --enable-test-discovery
10+
swift test -c debug --filter CorrectnessTests
1111

1212
test-performance:
1313
echo "" > benchmark.txt
14-
HYPHENATION_TEST_TYPE=performance swift test -c release --enable-test-discovery > >(tee -a benchmark.txt) 2> >(tee -a benchmark.txt >&2)
14+
swift test -c release --filter PerformanceTests > >(tee -a benchmark.txt) 2> >(tee -a benchmark.txt >&2)
1515
grep -oE 'test.+?average: [0-9]+\.[0-9]+' benchmark.txt | sed -E "s/]?'.+av/ av/g"
1616

1717
test-thread-safety:
18-
HYPHENATION_TEST_TYPE=thread-safety swift test -c debug --sanitize=thread --enable-test-discovery
18+
swift test -c debug --filter ThreadSafetyTests --sanitize=thread
1919

2020
publish: test
2121
rm -rf .docs

Package.swift

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.1
1+
// swift-tools-version:5.4
22

33
// Hyphenation
44
// © 2020 John Mueller
@@ -15,24 +15,10 @@ let package = Package(
1515
],
1616
targets: [
1717
.target(name: "Hyphenation"),
18-
.testTarget(name: "HyphenationTests", dependencies: ["Hyphenation"]),
19-
.testTarget(name: "PerformanceTests", dependencies: ["Hyphenation"]),
20-
.testTarget(name: "ThreadSafetyTests", dependencies: ["Hyphenation"]),
18+
.testTarget(
19+
name: "HyphenationTests",
20+
dependencies: ["Hyphenation"],
21+
resources: [.process("TextFiles")]
22+
)
2123
]
2224
)
23-
24-
import class Foundation.ProcessInfo
25-
26-
// conditional application of test targets based on environment variable
27-
switch ProcessInfo.processInfo.environment["HYPHENATION_TEST_TYPE"]?.lowercased() {
28-
case "hyphenation":
29-
package.targets.removeAll(where: { $0.type == .test && $0.name != "HyphenationTests" })
30-
case "performance":
31-
package.targets.removeAll(where: { $0.type == .test && $0.name != "HyphenationTests" })
32-
package.targets.first(where: { $0.type == .test })?.path = "Tests/PerformanceTests"
33-
case "thread-safety":
34-
package.targets.removeAll(where: { $0.type == .test && $0.name != "HyphenationTests" })
35-
package.targets.first(where: { $0.type == .test })?.path = "Tests/ThreadSafetyTests"
36-
default:
37-
break
38-
}

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Hyphenation is installed via the [Swift Package Manager](https://swift.org/packa
3737
let package = Package(
3838
...
3939
dependencies: [
40-
.package(url: "https://github.com/john-mueller/Hyphenation.git", from: "0.1.0")
40+
.package(url: "https://github.com/john-mueller/Hyphenation.git", from: "0.2.0")
4141
],
4242
...
4343
)
@@ -149,9 +149,8 @@ When filing an issue, please do your best to provide reproducable steps and an e
149149
In the case of a pull request, please take note of the following steps:
150150

151151
1. `swiftlint` should produce no warnings when run in the project directory. This is checked by CI, but I also recommend linting locally if possible (instructions for installation in the [SwiftLint repo](https://github.com/realm/SwiftLint#installation)).
152-
2. If you have added or renamed test cases, run `make generate-linuxmain` in the project directory. This will ensure all tests are run on both macOS and Linux.
153-
3. Make sure `make test` results in no errors. This runs the tests in the `HyphenationTests` and `ThreadSafetyTests` targets.
154-
4. If changing any internal implementations, please run `make bench` both with and without your changes, to check for any speed regressions. This runs the tests in the `PerformanceTests` target.
152+
2. Make sure `make test` results in no errors. This runs the tests in the `CorrectnessTests` and `ThreadSafetyTests` files.
153+
3. If changing any internal implementations, please run `make bench` both with and without your changes, to check for any speed regressions. This runs the tests in the `PerformanceTests` file.
155154

156155
## License
157156

Sources/Hyphenation/API/Hyphenator.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ public final class Hyphenator {
5757
// MARK: Properties
5858

5959
/// The minimum character count for a word to be hyphenated.
60-
public var minLength = 5 { didSet { clearCache() } }
60+
public var minLength: UInt = 5 { didSet { clearCache() } }
6161
/// The minimum number of characters between the beginning of the word and a hyphenation point.
62-
public var minLeading = 2 { didSet { clearCache() } }
62+
public var minLeading: UInt = 2 { didSet { clearCache() } }
6363
/// The minimum number of characters between the end of the word and a hyphenation point.
64-
public var minTrailing = 3 { didSet { clearCache() } }
64+
public var minTrailing: UInt = 3 { didSet { clearCache() } }
6565
/// The character to insert at each hyphenation point.
6666
public var separator: Character = "\u{00AD}" { didSet { clearCache() } }
6767

@@ -313,8 +313,8 @@ extension Hyphenator {
313313
/// - priorities: The mapping from pattern locations to hyphenation priorities.
314314
private func separating(_ word: String, using priorities: [Location: Priority]) -> String {
315315
var hyphenatedWord = word
316-
let minIndex = word.index(word.startIndex, offsetBy: minLeading)
317-
let maxIndex = word.index(word.endIndex, offsetBy: -minTrailing)
316+
let minIndex = word.index(word.startIndex, offsetBy: minLeading.clampedBetween(low: 0, high: word.count))
317+
let maxIndex = word.index(word.endIndex, offsetBy: -minTrailing.clampedBetween(low: 0, high: word.count))
318318

319319
for location in priorities.filter({ location, priority in
320320
if priority.isMultiple(of: 2) { return false }
@@ -329,6 +329,16 @@ extension Hyphenator {
329329
}
330330
}
331331

332+
// MARK: Validation Extensions
333+
334+
extension UInt {
335+
/// Converts to `Int` safely and clamps result between two values (inclusive).
336+
fileprivate func clampedBetween(low: Int, high: Int) -> Int {
337+
let thing = (self <= Int.max) ? Int(self) : Int.max
338+
return Swift.max(low, Swift.min(high, thing))
339+
}
340+
}
341+
332342
// MARK: Typealiases
333343

334344
extension Hyphenator {

Sources/Hyphenation/API/PatternParsingError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public enum PatternParsingError: Error, Equatable {
1111
/// A pattern string which did not contain at least one letter and one digit from between 1 and 9, inclusive.
1212
case deficientPattern(String)
13-
/// A pattern string which contained a character that was not alphabetic, a digits 1-9, or a period.
13+
/// A pattern string which contained a character that was not alphabetic, a digit 1-9, or a period.
1414
case invalidCharacter(String)
1515
/// A pattern string which contained multiple consecutive digits.
1616
case consecutiveDigits(String)

Sources/Hyphenation/Internal/ExceptionDictionary.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,6 @@ extension ExceptionDictionary {
134134
// MARK: Default exceptions
135135

136136
extension ExceptionDictionary {
137-
/// A URL refering to a file containing the default English exceptions.
138-
private static let defaultFileURL = URL(fileURLWithPath: #file)
139-
.deletingLastPathComponent()
140-
.deletingLastPathComponent()
141-
.appendingPathComponent("Patterns/hyph-en-us.hyp.txt")
142-
143137
/// An `ExceptionDictionary` containing the default English exceptions.
144-
private static var defaultDictionary = try! ExceptionDictionary(fileURL: defaultFileURL)
145-
// swiftlint:disable:previous force_try
138+
private static var defaultDictionary = ExceptionDictionary(string: .defaultExceptions)
146139
}

Sources/Hyphenation/Internal/PatternDictionary.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,8 @@ internal final class PatternDictionary: Copyable {
8383
// MARK: Default patterns
8484

8585
extension PatternDictionary {
86-
/// A URL refering to a file containing the default English patterns.
87-
private static let defaultFileURL = URL(fileURLWithPath: #file)
88-
.deletingLastPathComponent()
89-
.deletingLastPathComponent()
90-
.appendingPathComponent("Patterns/hyph-en-us.pat.txt")
91-
9286
/// A `PatternDictionary` containing the default English patterns.
93-
private static var defaultDictionary = try! PatternDictionary(fileURL: defaultFileURL)
87+
private static var defaultDictionary = try! PatternDictionary(string: .defaultPatterns)
9488
// swiftlint:disable:previous force_try
9589

9690
/// The length of the longest identifier in the default dictionary.

0 commit comments

Comments
 (0)