Skip to content

Commit

Permalink
swift no-throw apis
Browse files Browse the repository at this point in the history
  • Loading branch information
paiv committed Dec 26, 2024
1 parent 1ccb07c commit 2d2a065
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 99 deletions.
8 changes: 4 additions & 4 deletions docs/_posts/2024-11-13-uklatn-swift.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ Usage
```swift
import UkrainianLatin

try encode("Доброго вечора!")
try decode("Paljanycja")
encode("Доброго вечора!")
decode("Paljanycja")
```

Set the transliteration scheme:
```swift
try encode("Борщ", table: UKLatnTable.DSTU_9112_B)
try encode("Шевченко", table: UKLatnTable.KMU_55)
encode("Борщ", table: UKLatnTable.DSTU_9112_B)
encode("Шевченко", table: UKLatnTable.KMU_55)
```


Expand Down
29 changes: 12 additions & 17 deletions swift/Sources/UkrainianLatin/UKLatn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
import Foundation


public enum UKLatnError: Error, Equatable {
case invalidTable(Int)
}


/// Transliterates a string of Ukrainian Cyrillic to Latin script.
///
/// - Parameters:
Expand All @@ -19,12 +14,12 @@ public enum UKLatnError: Error, Equatable {
/// - `KMU_55`: KMU 55:2010
/// - Returns: The transliterated string.
@Sendable
public func encode(_ text: String, table: UKLatnTable = .DSTU_9112_A) throws -> String {
public func encode(_ text: String, table: UKLatnTable = .DSTU_9112_A) -> String {
guard let transform = _UklatnTables[table]?.encode
else {
throw UKLatnError.invalidTable(table.rawValue)
preconditionFailure("invalid encoding table \(table)")
}
return try transform(text)
return transform(text)
}


Expand All @@ -37,12 +32,12 @@ public func encode(_ text: String, table: UKLatnTable = .DSTU_9112_A) throws ->
/// - `DSTU_9112_B`: DSTU 9112:2021 System B
/// - Returns: The transliterated string.
@Sendable
public func decode(_ text: String, table: UKLatnTable = .DSTU_9112_A) throws -> String {
public func decode(_ text: String, table: UKLatnTable = .DSTU_9112_A) -> String {
guard let transform = _UklatnTables[table]?.decode
else {
throw UKLatnError.invalidTable(table.rawValue)
preconditionFailure("invalid decoding table \(table)")
}
return try transform(text)
return transform(text)
}


Expand Down Expand Up @@ -91,7 +86,7 @@ public enum UKLatnTable : Int, Sendable {


private struct _UKLatnCodec : Sendable {
typealias Transform = (@Sendable (String) throws -> String)
typealias Transform = (@Sendable (String) -> String)
let encode: Transform?
let decode: Transform?
}
Expand All @@ -107,7 +102,7 @@ private let _Uklatn_uk_uk_Latn_DSTU_9112_A: @Sendable () -> _UKLatnCodec.Transfo
]

@Sendable
func transform(_ text: String) throws -> String {
func transform(_ text: String) -> String {
var text = text
text = text.precomposedStringWithCanonicalMapping // NFC
text = text.replacing(_rx1) { (i, match) in
Expand All @@ -129,7 +124,7 @@ private let _Uklatn_uk_uk_Latn_DSTU_9112_B: @Sendable () -> _UKLatnCodec.Transfo
]

@Sendable
func transform(_ text: String) throws -> String {
func transform(_ text: String) -> String {
var text = text
text = text.precomposedStringWithCanonicalMapping // NFC
text = text.replacing(_rx1) { (i, match) in
Expand Down Expand Up @@ -157,7 +152,7 @@ private let _Uklatn_uk_uk_Latn_KMU_55: @Sendable () -> _UKLatnCodec.Transform =
]

@Sendable
func transform(_ text: String) throws -> String {
func transform(_ text: String) -> String {
var text = text
text = text.precomposedStringWithCanonicalMapping // NFC
text = text.replacing(_rx1) { (i, match) in
Expand Down Expand Up @@ -187,7 +182,7 @@ private let _Uklatn_uk_Latn_DSTU_9112_A_uk: @Sendable () -> _UKLatnCodec.Transfo
]

@Sendable
func transform(_ text: String) throws -> String {
func transform(_ text: String) -> String {
var text = text
text = text.precomposedStringWithCanonicalMapping // NFC
text = text.replacing(_rx1) { (i, match) in
Expand Down Expand Up @@ -219,7 +214,7 @@ private let _Uklatn_uk_Latn_DSTU_9112_B_uk: @Sendable () -> _UKLatnCodec.Transfo
]

@Sendable
func transform(_ text: String) throws -> String {
func transform(_ text: String) -> String {
var text = text
text = text.precomposedStringWithCanonicalMapping // NFC
text = text.replacing(_rx1) { (i, match) in
Expand Down
15 changes: 6 additions & 9 deletions swift/Sources/cli/uklatn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,38 @@ struct MyApp {
try transformFile(file, direction: args.direction, table: args.table)
}
else if let text = args.text {
try transformText(text, direction: args.direction, table: args.table)
transformText(text, direction: args.direction, table: args.table)
}
}
catch let error as AppArgs.ParseError {
AppArgs.printError(error, to: &stderr)
}
catch UKLatnError.invalidTable(let table) {
AppArgs.printError(.invalidTable("\(table)"), to: &stderr)
}
catch {
print(error, to: &stderr)
}
}

private static func transformText(_ text: String, direction: AppArgs.TransformDirection, table: UKLatnTable) throws {
private static func transformText(_ text: String, direction: AppArgs.TransformDirection, table: UKLatnTable) {
let value: String
switch direction {
case .cyr2lat:
value = try encode(text, table: table)
value = encode(text, table: table)
case .lat2cyr:
value = try decode(text, table: table)
value = decode(text, table: table)
}
print("\(value)")
}

private static func transformFile(_ file: String, direction: AppArgs.TransformDirection, table: UKLatnTable) throws {
if file == "-" {
while let text = readLine() {
try transformText(text, direction: direction, table: table)
transformText(text, direction: direction, table: table)
}
}
else {
var encoding: String.Encoding = .utf8
let text = try String(contentsOfFile: file, usedEncoding: &encoding)
try transformText(text, direction: direction, table: table)
transformText(text, direction: direction, table: table)
}
}
}
Expand Down
48 changes: 20 additions & 28 deletions swift/Tests/UkrainianLatinTests/UKLatnTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import XCTest

class Dstu9112ATests: XCTestCase {

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

for (cyr, lat) in data {
let enc = try encode(cyr, table: UKLatnTable.DSTU_9112_A)
let enc = encode(cyr, table: UKLatnTable.DSTU_9112_A)
XCTAssertEqual(lat, enc)
let dec = try decode(lat, table: UKLatnTable.DSTU_9112_A)
let dec = decode(lat, table: UKLatnTable.DSTU_9112_A)
XCTAssertEqual(cyr, dec)
}

for (cyr, lat) in data {
let enc = try encode(cyr)
let enc = encode(cyr)
XCTAssertEqual(lat, enc)
let dec = try decode(lat)
let dec = decode(lat)
XCTAssertEqual(cyr, dec)
}
}

func test_c2l_DSTU_9112_A() throws {
func test_c2l_DSTU_9112_A() {
let data: [(String, String)] = [
(
"в’я в'я",
Expand All @@ -106,17 +106,17 @@ class Dstu9112ATests: XCTestCase {
]

for (cyr, lat) in data {
let enc = try encode(cyr, table: UKLatnTable.DSTU_9112_A)
let enc = encode(cyr, table: UKLatnTable.DSTU_9112_A)
XCTAssertEqual(lat, enc)
}

for (cyr, lat) in data {
let enc = try encode(cyr)
let enc = encode(cyr)
XCTAssertEqual(lat, enc)
}
}

func test_l2c_DSTU_9112_A() throws {
func test_l2c_DSTU_9112_A() {
let data: [(String, String)] = [
(
"я є ю",
Expand All @@ -141,12 +141,12 @@ class Dstu9112ATests: XCTestCase {
]

for (cyr, lat) in data {
let dec = try decode(lat, table: UKLatnTable.DSTU_9112_A)
let dec = decode(lat, table: UKLatnTable.DSTU_9112_A)
XCTAssertEqual(cyr, dec)
}

for (cyr, lat) in data {
let dec = try decode(lat)
let dec = decode(lat)
XCTAssertEqual(cyr, dec)
}
}
Expand All @@ -155,7 +155,7 @@ class Dstu9112ATests: XCTestCase {

class Dstu9112BTests: XCTestCase {

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

for (cyr, lat) in data {
let enc = try encode(cyr, table: UKLatnTable.DSTU_9112_B)
let enc = encode(cyr, table: UKLatnTable.DSTU_9112_B)
XCTAssertEqual(lat, enc)
let dec = try decode(lat, table: UKLatnTable.DSTU_9112_B)
let dec = decode(lat, table: UKLatnTable.DSTU_9112_B)
XCTAssertEqual(cyr, dec)
}
}

func test_c2l_DSTU_9112_B() throws {
func test_c2l_DSTU_9112_B() {
let data: [(String, String)] = [
(
"в’я в'я",
Expand All @@ -248,12 +248,12 @@ class Dstu9112BTests: XCTestCase {
]

for (cyr, lat) in data {
let enc = try encode(cyr, table: UKLatnTable.DSTU_9112_B)
let enc = encode(cyr, table: UKLatnTable.DSTU_9112_B)
XCTAssertEqual(lat, enc)
}
}

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

for (cyr, lat) in data {
let dec = try decode(lat, table: UKLatnTable.DSTU_9112_B)
let dec = decode(lat, table: UKLatnTable.DSTU_9112_B)
XCTAssertEqual(cyr, dec)
}
}
Expand All @@ -279,7 +279,7 @@ class Dstu9112BTests: XCTestCase {

class Kmu55Tests: XCTestCase {

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

for (cyr, lat) in data {
let enc = try encode(cyr, table: UKLatnTable.KMU_55)
let enc = encode(cyr, table: UKLatnTable.KMU_55)
XCTAssertEqual(lat, enc)
}
}

func test_decode_KMU_55_throws() throws {
XCTAssertThrowsError(
try decode("lat", table: UKLatnTable.KMU_55)
) { error in
XCTAssertEqual(error as? UKLatnError, UKLatnError.invalidTable(UKLatnTable.KMU_55.rawValue))
}
}
}
8 changes: 4 additions & 4 deletions swift/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ Supported transliteration schemes:
```swift
import UkrainianLatin

try encode("Доброго вечора!")
try decode("Paljanycja")
encode("Доброго вечора!")
decode("Paljanycja")
```

Set the transliteration scheme:
```swift
try encode("Борщ", table: UKLatnTable.DSTU_9112_B)
try encode("Шевченко", table: UKLatnTable.KMU_55)
encode("Борщ", table: UKLatnTable.DSTU_9112_B)
encode("Шевченко", table: UKLatnTable.KMU_55)
```


Expand Down
Loading

0 comments on commit 2d2a065

Please sign in to comment.