Skip to content
/ uklatn Public

Commit 2d2a065

Browse files
committed
swift no-throw apis
1 parent 1ccb07c commit 2d2a065

File tree

6 files changed

+64
-99
lines changed

6 files changed

+64
-99
lines changed

docs/_posts/2024-11-13-uklatn-swift.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ Usage
2222
```swift
2323
import UkrainianLatin
2424

25-
try encode("Доброго вечора!")
26-
try decode("Paljanycja")
25+
encode("Доброго вечора!")
26+
decode("Paljanycja")
2727
```
2828

2929
Set the transliteration scheme:
3030
```swift
31-
try encode("Борщ", table: UKLatnTable.DSTU_9112_B)
32-
try encode("Шевченко", table: UKLatnTable.KMU_55)
31+
encode("Борщ", table: UKLatnTable.DSTU_9112_B)
32+
encode("Шевченко", table: UKLatnTable.KMU_55)
3333
```
3434

3535

swift/Sources/UkrainianLatin/UKLatn.swift

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
import Foundation
55

66

7-
public enum UKLatnError: Error, Equatable {
8-
case invalidTable(Int)
9-
}
10-
11-
127
/// Transliterates a string of Ukrainian Cyrillic to Latin script.
138
///
149
/// - Parameters:
@@ -19,12 +14,12 @@ public enum UKLatnError: Error, Equatable {
1914
/// - `KMU_55`: KMU 55:2010
2015
/// - Returns: The transliterated string.
2116
@Sendable
22-
public func encode(_ text: String, table: UKLatnTable = .DSTU_9112_A) throws -> String {
17+
public func encode(_ text: String, table: UKLatnTable = .DSTU_9112_A) -> String {
2318
guard let transform = _UklatnTables[table]?.encode
2419
else {
25-
throw UKLatnError.invalidTable(table.rawValue)
20+
preconditionFailure("invalid encoding table \(table)")
2621
}
27-
return try transform(text)
22+
return transform(text)
2823
}
2924

3025

@@ -37,12 +32,12 @@ public func encode(_ text: String, table: UKLatnTable = .DSTU_9112_A) throws ->
3732
/// - `DSTU_9112_B`: DSTU 9112:2021 System B
3833
/// - Returns: The transliterated string.
3934
@Sendable
40-
public func decode(_ text: String, table: UKLatnTable = .DSTU_9112_A) throws -> String {
35+
public func decode(_ text: String, table: UKLatnTable = .DSTU_9112_A) -> String {
4136
guard let transform = _UklatnTables[table]?.decode
4237
else {
43-
throw UKLatnError.invalidTable(table.rawValue)
38+
preconditionFailure("invalid decoding table \(table)")
4439
}
45-
return try transform(text)
40+
return transform(text)
4641
}
4742

4843

@@ -91,7 +86,7 @@ public enum UKLatnTable : Int, Sendable {
9186

9287

9388
private struct _UKLatnCodec : Sendable {
94-
typealias Transform = (@Sendable (String) throws -> String)
89+
typealias Transform = (@Sendable (String) -> String)
9590
let encode: Transform?
9691
let decode: Transform?
9792
}
@@ -107,7 +102,7 @@ private let _Uklatn_uk_uk_Latn_DSTU_9112_A: @Sendable () -> _UKLatnCodec.Transfo
107102
]
108103

109104
@Sendable
110-
func transform(_ text: String) throws -> String {
105+
func transform(_ text: String) -> String {
111106
var text = text
112107
text = text.precomposedStringWithCanonicalMapping // NFC
113108
text = text.replacing(_rx1) { (i, match) in
@@ -129,7 +124,7 @@ private let _Uklatn_uk_uk_Latn_DSTU_9112_B: @Sendable () -> _UKLatnCodec.Transfo
129124
]
130125

131126
@Sendable
132-
func transform(_ text: String) throws -> String {
127+
func transform(_ text: String) -> String {
133128
var text = text
134129
text = text.precomposedStringWithCanonicalMapping // NFC
135130
text = text.replacing(_rx1) { (i, match) in
@@ -157,7 +152,7 @@ private let _Uklatn_uk_uk_Latn_KMU_55: @Sendable () -> _UKLatnCodec.Transform =
157152
]
158153

159154
@Sendable
160-
func transform(_ text: String) throws -> String {
155+
func transform(_ text: String) -> String {
161156
var text = text
162157
text = text.precomposedStringWithCanonicalMapping // NFC
163158
text = text.replacing(_rx1) { (i, match) in
@@ -187,7 +182,7 @@ private let _Uklatn_uk_Latn_DSTU_9112_A_uk: @Sendable () -> _UKLatnCodec.Transfo
187182
]
188183

189184
@Sendable
190-
func transform(_ text: String) throws -> String {
185+
func transform(_ text: String) -> String {
191186
var text = text
192187
text = text.precomposedStringWithCanonicalMapping // NFC
193188
text = text.replacing(_rx1) { (i, match) in
@@ -219,7 +214,7 @@ private let _Uklatn_uk_Latn_DSTU_9112_B_uk: @Sendable () -> _UKLatnCodec.Transfo
219214
]
220215

221216
@Sendable
222-
func transform(_ text: String) throws -> String {
217+
func transform(_ text: String) -> String {
223218
var text = text
224219
text = text.precomposedStringWithCanonicalMapping // NFC
225220
text = text.replacing(_rx1) { (i, match) in

swift/Sources/cli/uklatn.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,38 @@ struct MyApp {
1717
try transformFile(file, direction: args.direction, table: args.table)
1818
}
1919
else if let text = args.text {
20-
try transformText(text, direction: args.direction, table: args.table)
20+
transformText(text, direction: args.direction, table: args.table)
2121
}
2222
}
2323
catch let error as AppArgs.ParseError {
2424
AppArgs.printError(error, to: &stderr)
2525
}
26-
catch UKLatnError.invalidTable(let table) {
27-
AppArgs.printError(.invalidTable("\(table)"), to: &stderr)
28-
}
2926
catch {
3027
print(error, to: &stderr)
3128
}
3229
}
3330

34-
private static func transformText(_ text: String, direction: AppArgs.TransformDirection, table: UKLatnTable) throws {
31+
private static func transformText(_ text: String, direction: AppArgs.TransformDirection, table: UKLatnTable) {
3532
let value: String
3633
switch direction {
3734
case .cyr2lat:
38-
value = try encode(text, table: table)
35+
value = encode(text, table: table)
3936
case .lat2cyr:
40-
value = try decode(text, table: table)
37+
value = decode(text, table: table)
4138
}
4239
print("\(value)")
4340
}
4441

4542
private static func transformFile(_ file: String, direction: AppArgs.TransformDirection, table: UKLatnTable) throws {
4643
if file == "-" {
4744
while let text = readLine() {
48-
try transformText(text, direction: direction, table: table)
45+
transformText(text, direction: direction, table: table)
4946
}
5047
}
5148
else {
5249
var encoding: String.Encoding = .utf8
5350
let text = try String(contentsOfFile: file, usedEncoding: &encoding)
54-
try transformText(text, direction: direction, table: table)
51+
transformText(text, direction: direction, table: table)
5552
}
5653
}
5754
}

swift/Tests/UkrainianLatinTests/UKLatnTests.swift

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import XCTest
66

77
class Dstu9112ATests: XCTestCase {
88

9-
func test_c2lr_DSTU_9112_A() throws {
9+
func test_c2lr_DSTU_9112_A() {
1010
let data: [(String, String)] = [
1111
(
1212
"Україна, Хмельницький",
@@ -79,21 +79,21 @@ class Dstu9112ATests: XCTestCase {
7979
]
8080

8181
for (cyr, lat) in data {
82-
let enc = try encode(cyr, table: UKLatnTable.DSTU_9112_A)
82+
let enc = encode(cyr, table: UKLatnTable.DSTU_9112_A)
8383
XCTAssertEqual(lat, enc)
84-
let dec = try decode(lat, table: UKLatnTable.DSTU_9112_A)
84+
let dec = decode(lat, table: UKLatnTable.DSTU_9112_A)
8585
XCTAssertEqual(cyr, dec)
8686
}
8787

8888
for (cyr, lat) in data {
89-
let enc = try encode(cyr)
89+
let enc = encode(cyr)
9090
XCTAssertEqual(lat, enc)
91-
let dec = try decode(lat)
91+
let dec = decode(lat)
9292
XCTAssertEqual(cyr, dec)
9393
}
9494
}
9595

96-
func test_c2l_DSTU_9112_A() throws {
96+
func test_c2l_DSTU_9112_A() {
9797
let data: [(String, String)] = [
9898
(
9999
"в’я в'я",
@@ -106,17 +106,17 @@ class Dstu9112ATests: XCTestCase {
106106
]
107107

108108
for (cyr, lat) in data {
109-
let enc = try encode(cyr, table: UKLatnTable.DSTU_9112_A)
109+
let enc = encode(cyr, table: UKLatnTable.DSTU_9112_A)
110110
XCTAssertEqual(lat, enc)
111111
}
112112

113113
for (cyr, lat) in data {
114-
let enc = try encode(cyr)
114+
let enc = encode(cyr)
115115
XCTAssertEqual(lat, enc)
116116
}
117117
}
118118

119-
func test_l2c_DSTU_9112_A() throws {
119+
func test_l2c_DSTU_9112_A() {
120120
let data: [(String, String)] = [
121121
(
122122
"я є ю",
@@ -141,12 +141,12 @@ class Dstu9112ATests: XCTestCase {
141141
]
142142

143143
for (cyr, lat) in data {
144-
let dec = try decode(lat, table: UKLatnTable.DSTU_9112_A)
144+
let dec = decode(lat, table: UKLatnTable.DSTU_9112_A)
145145
XCTAssertEqual(cyr, dec)
146146
}
147147

148148
for (cyr, lat) in data {
149-
let dec = try decode(lat)
149+
let dec = decode(lat)
150150
XCTAssertEqual(cyr, dec)
151151
}
152152
}
@@ -155,7 +155,7 @@ class Dstu9112ATests: XCTestCase {
155155

156156
class Dstu9112BTests: XCTestCase {
157157

158-
func test_c2lr_DSTU_9112_B() throws {
158+
func test_c2lr_DSTU_9112_B() {
159159
let data: [(String, String)] = [
160160
(
161161
"Україна, Хмельницький",
@@ -228,14 +228,14 @@ class Dstu9112BTests: XCTestCase {
228228
]
229229

230230
for (cyr, lat) in data {
231-
let enc = try encode(cyr, table: UKLatnTable.DSTU_9112_B)
231+
let enc = encode(cyr, table: UKLatnTable.DSTU_9112_B)
232232
XCTAssertEqual(lat, enc)
233-
let dec = try decode(lat, table: UKLatnTable.DSTU_9112_B)
233+
let dec = decode(lat, table: UKLatnTable.DSTU_9112_B)
234234
XCTAssertEqual(cyr, dec)
235235
}
236236
}
237237

238-
func test_c2l_DSTU_9112_B() throws {
238+
func test_c2l_DSTU_9112_B() {
239239
let data: [(String, String)] = [
240240
(
241241
"в’я в'я",
@@ -248,12 +248,12 @@ class Dstu9112BTests: XCTestCase {
248248
]
249249

250250
for (cyr, lat) in data {
251-
let enc = try encode(cyr, table: UKLatnTable.DSTU_9112_B)
251+
let enc = encode(cyr, table: UKLatnTable.DSTU_9112_B)
252252
XCTAssertEqual(lat, enc)
253253
}
254254
}
255255

256-
func test_l2c_DSTU_9112_B() throws {
256+
func test_l2c_DSTU_9112_B() {
257257
let data: [(String, String)] = [
258258
(
259259
"я ї є ю г ж х щ ш ч ь",
@@ -270,7 +270,7 @@ class Dstu9112BTests: XCTestCase {
270270
]
271271

272272
for (cyr, lat) in data {
273-
let dec = try decode(lat, table: UKLatnTable.DSTU_9112_B)
273+
let dec = decode(lat, table: UKLatnTable.DSTU_9112_B)
274274
XCTAssertEqual(cyr, dec)
275275
}
276276
}
@@ -279,7 +279,7 @@ class Dstu9112BTests: XCTestCase {
279279

280280
class Kmu55Tests: XCTestCase {
281281

282-
func test_c2l_KMU_55() throws {
282+
func test_c2l_KMU_55() {
283283
let data: [(String, String)] = [
284284
(
285285
"Україна, Хмельницький",
@@ -352,16 +352,8 @@ class Kmu55Tests: XCTestCase {
352352
]
353353

354354
for (cyr, lat) in data {
355-
let enc = try encode(cyr, table: UKLatnTable.KMU_55)
355+
let enc = encode(cyr, table: UKLatnTable.KMU_55)
356356
XCTAssertEqual(lat, enc)
357357
}
358358
}
359-
360-
func test_decode_KMU_55_throws() throws {
361-
XCTAssertThrowsError(
362-
try decode("lat", table: UKLatnTable.KMU_55)
363-
) { error in
364-
XCTAssertEqual(error as? UKLatnError, UKLatnError.invalidTable(UKLatnTable.KMU_55.rawValue))
365-
}
366-
}
367359
}

swift/readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ Supported transliteration schemes:
1010
```swift
1111
import UkrainianLatin
1212

13-
try encode("Доброго вечора!")
14-
try decode("Paljanycja")
13+
encode("Доброго вечора!")
14+
decode("Paljanycja")
1515
```
1616

1717
Set the transliteration scheme:
1818
```swift
19-
try encode("Борщ", table: UKLatnTable.DSTU_9112_B)
20-
try encode("Шевченко", table: UKLatnTable.KMU_55)
19+
encode("Борщ", table: UKLatnTable.DSTU_9112_B)
20+
encode("Шевченко", table: UKLatnTable.KMU_55)
2121
```
2222

2323

0 commit comments

Comments
 (0)