Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend ability for handing JSON dictionaries. #1

Merged
merged 2 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions .clang-format-swift.json

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,3 @@ _TeamCity*
_UpgradeReport_Files/
__pycache__/
~$*
Tests/TestD[Ii]ctData/convdict.plist
104 changes: 104 additions & 0 deletions .swiftformat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# SwiftFormat config compliant with Google Swift Guideline
# https://google.github.io/swift/#control-flow-statements

# Specify version used in a project

--swiftversion 5.5

# Rules explicitly required by the guideline

--rules \
blankLinesAroundMark, \
blankLinesAtEndOfScope, \
blankLinesAtStartOfScope, \
blankLinesBetweenScopes, \
braces, \
consecutiveBlankLines, \
consecutiveSpaces, \
duplicateImports, \
elseOnSameLine, \
emptyBraces, \
enumNamespaces, \
extensionAccessControl, \
hoistPatternLet, \
indent, \
leadingDelimiters, \
linebreakAtEndOfFile, \
markTypes, \
organizeDeclarations, \
redundantInit, \
redundantParens, \
redundantPattern, \
redundantRawValues, \
redundantType, \
redundantVoidReturnType, \
semicolons, \
sortedImports, \
sortedSwitchCases, \
spaceAroundBraces, \
spaceAroundBrackets, \
spaceAroundComments, \
spaceAroundGenerics, \
spaceAroundOperators, \
spaceAroundParens, \
spaceInsideBraces, \
spaceInsideBrackets, \
spaceInsideComments, \
spaceInsideGenerics, \
spaceInsideParens, \
todos, \
trailingClosures, \
trailingCommas, \
trailingSpace, \
typeSugar, \
void, \
wrap, \
wrapArguments, \
wrapAttributes, \
#
#
# Additional rules not mentioned in the guideline, but helping to keep the codebase clean
# Quoting the guideline:
# Common themes among the rules in this section are:
# avoid redundancy, avoid ambiguity, and prefer implicitness over explicitness
# unless being explicit improves readability and/or reduces ambiguity.
#
#
andOperator, \
isEmpty, \
redundantBackticks, \
redundantBreak, \
redundantExtensionACL, \
redundantGet, \
redundantLetError, \
redundantNilInit, \
redundantObjc, \
redundantReturn, \
redundantSelf, \
strongifiedSelf


# Options for basic rules

--extensionacl on-declarations
--funcattributes prev-line
--indent 2
--maxwidth 100
--typeattributes prev-line
--varattributes prev-line
--voidtype tuple
--wraparguments before-first
--wrapparameters before-first
--wrapcollections before-first
--wrapreturntype if-multiline
--wrapconditions after-first

# Option for additional rules

--self init-only

# Excluded folders

--exclude Pods,**/UNTESTED_TODO,vendor,fastlane

# https://github.com/NoemiRozpara/Google-SwiftFormat-Config
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let package = Package(
.library(
name: "Hotenka",
targets: ["Hotenka"]
)
),
],
dependencies: [],
targets: [
Expand Down
96 changes: 55 additions & 41 deletions Sources/Hotenka/HotenkaChineseConverter.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// Swiftified by (c) 2022 and onwards The vChewing Project (MIT-NTL License).
// Rebranded from (c) Nick Chen's Obj-C library "NCChineseConverter" (MIT License).
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

2. No trademark license is granted to use the trade names, trademarks, service
marks, or product names of Contributor, except as required to fulfill notice
requirements above.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

2. No trademark license is granted to use the trade names, trademarks, service
marks, or product names of Contributor, except as required to fulfill notice
requirements above.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import Foundation

Expand Down Expand Up @@ -57,6 +57,20 @@ public class HotenkaChineseConverter {
}
}

public init(jsonDir: String) {
dictFiles = .init()
do {
let rawData = try Data(contentsOf: URL(fileURLWithPath: jsonDir))
guard let rawJSON: [String: [String: String]] = try JSONSerialization.jsonObject(with: rawData) as? [String: [String: String]] else {
throw NSError()
}
dict = rawJSON
} catch {
NSLog("// Exception happened when reading dict json at: \(jsonDir).")
dict = .init()
}
}

public init(dictDir: String) {
dictFiles = [
"zh2TW": [String](),
Expand Down Expand Up @@ -127,33 +141,33 @@ public class HotenkaChineseConverter {
var dictTypeKey: String

switch dictType {
case .zhHantTW:
dictTypeKey = "zh2TW"
case .zhHantHK:
dictTypeKey = "zh2HK"
case .zhHansSG:
dictTypeKey = "zh2SG"
case .zhHansJP:
dictTypeKey = "zh2JP"
case .zhHantKX:
dictTypeKey = "zh2KX"
case .zhHansCN:
dictTypeKey = "zh2CN"
case .zhHantTW:
dictTypeKey = "zh2TW"
case .zhHantHK:
dictTypeKey = "zh2HK"
case .zhHansSG:
dictTypeKey = "zh2SG"
case .zhHansJP:
dictTypeKey = "zh2JP"
case .zhHantKX:
dictTypeKey = "zh2KX"
case .zhHansCN:
dictTypeKey = "zh2CN"
}

var result = ""
guard let useDict = dict[dictTypeKey] else { return target }

var i = 0
outerloop: while i < (target.count) {
while i < (target.count) {
let max = (target.count) - i
var j: Int
j = max

innerloop: while j > 0 {
let start = target.index(target.startIndex, offsetBy: i)
let end = target.index(target.startIndex, offsetBy: i + j)
guard let useDictSubStr = useDict[String(target[start..<end])] else {
guard let useDictSubStr = useDict[String(target[start ..< end])] else {
j -= 1
continue
}
Expand All @@ -164,7 +178,7 @@ public class HotenkaChineseConverter {
if j == 0 {
let start = target.index(target.startIndex, offsetBy: i)
let end = target.index(target.startIndex, offsetBy: i + 1)
result = result + String(target[start..<end])
result = result + String(target[start ..< end])
i += 1
} else {
i += j
Expand All @@ -177,18 +191,18 @@ public class HotenkaChineseConverter {

// MARK: - String extensions

extension String {
fileprivate func range(of str: String) -> Range<Int> {
private extension String {
func range(of str: String) -> Range<Int> {
var start = -1
withCString { bytes in
str.withCString { sbytes in
start = strstr(bytes, sbytes) - UnsafeMutablePointer<Int8>(mutating: bytes)
}
}
return start < 0 ? 0..<0 : start..<start + str.utf8.count
return start < 0 ? 0 ..< 0 : start ..< start + str.utf8.count
}

fileprivate func substring(to index: Int) -> String {
func substring(to index: Int) -> String {
var out = self
withCString { bytes in
let bytes = UnsafeMutablePointer<Int8>(mutating: bytes)
Expand All @@ -198,7 +212,7 @@ extension String {
return out
}

fileprivate func substring(from index: Int) -> String {
func substring(from index: Int) -> String {
var out = self
withCString { bytes in
out = String(cString: bytes + index)
Expand Down
63 changes: 63 additions & 0 deletions Tests/HotenkaTests/HotekaTests_JSON.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Swiftified by (c) 2022 and onwards The vChewing Project (MIT-NTL License).
// Rebranded from (c) Nick Chen's Obj-C library "NCChineseConverter" (MIT License).
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

2. No trademark license is granted to use the trade names, trademarks, service
marks, or product names of Contributor, except as required to fulfill notice
requirements above.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import Foundation
import XCTest

@testable import Hotenka

private let packageRootPath = URL(fileURLWithPath: #file).pathComponents.prefix(while: { $0 != "Tests" }).joined(
separator: "/"
).dropFirst()

private let testDataPath: String = packageRootPath + "/Tests/TestDictData/"

extension HotenkaTests {
func testGeneratingJSON() throws {
NSLog("// Start loading from: \(packageRootPath)")
let testInstance: HotenkaChineseConverter = .init(dictDir: testDataPath)
NSLog("// Loading complete. Generating json dict file.")
do {
try JSONSerialization.data(withJSONObject: testInstance.dict, options: .sortedKeys).write(to: URL(fileURLWithPath: testDataPath + "convdict.json"))
} catch {
NSLog("// Error on writing strings to file: \(error)")
}
}

func testSampleWithJSON() throws {
NSLog("// Start loading json from: \(packageRootPath)")
let testInstance2: HotenkaChineseConverter = .init(jsonDir: testDataPath + "convdict.json")
NSLog("// Successfully loading json dictionary.")

let oriString = "为中华崛起而读书"
let result1 = testInstance2.convert(oriString, to: .zhHantTW)
let result2 = testInstance2.convert(result1, to: .zhHantKX)
let result3 = testInstance2.convert(result2, to: .zhHansJP)
NSLog("// Results: \(result1) \(result2) \(result3)")
XCTAssertEqual(result1, "為中華崛起而讀書")
XCTAssertEqual(result2, "爲中華崛起而讀書")
XCTAssertEqual(result3, "為中華崛起而読書")
}
}
Loading