Skip to content

Commit 9d80bd3

Browse files
committed
add Character.getNumericValue (char)
1 parent 4553daf commit 9d80bd3

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

Sources/JavApi/JavApi⁴Swift.docc/Java2Swift.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@ The translation has a Optional problem because Java reference types are implicit
196196

197197
Default methods are implemented in a extension, because the mapping of Java interfaces in a Java package like structures need a bit more code.
198198

199+
##### toString() method
200+
201+
If special toString() implemetation is in Java, first let your Swift type be conform to the *CustomStringConvertible*. Next implement the property *description*. For example
202+
203+
```swift
204+
public func toString () -> String {
205+
return "My name is Bond, James Bond"
206+
}
207+
208+
public var description: String {
209+
get {
210+
return self.toString()
211+
}
212+
}
213+
```
214+
199215
#### nested classes
200216

201217
Nested classes in Swift can not access to enclosing type variables. Give the nested class the needed variables as (weak) parameters.
@@ -270,6 +286,23 @@ Swiftify is a JavApi extension. It make additional implementations of Java metho
270286

271287
Java cases `fallthrough` by default and need explicite `break` if not. But Java need no `default`. Also newer Java can use `->` instead of `:`.
272288

289+
If you use enum values in switch, you need a dot before the value name in case.
290+
291+
```java
292+
switch (enumType){
293+
case ENUM_NAME: break;
294+
default: break;
295+
}
296+
```
297+
298+
299+
```swift
300+
switch (enumType){
301+
case .ENUM_NAME: break;
302+
default: break;
303+
}
304+
```
305+
273306
#### visibility
274307

275308
Swift visiblilities are:

Sources/JavApi/lang/Character+Java.swift

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,67 @@ extension Character {
3333
asString = String(String.UnicodeScalarView(asArray))
3434
return isLetter(asString.toCharArray()[0])
3535
}
36-
36+
3737
public static func isWhitespace (_ char : char) -> boolean {
3838
return char.isWhitespace
3939
}
40+
41+
/// Return the numeric value of Unicode character, f.e. ``\u{216D}`` returns 100.
42+
///
43+
/// - Parameters: char
44+
/// - Returns: numeric value or 10 to 35 for A-Z or -2 for fractions or -1 for all others
45+
public static func getNumericValue (_ char : Character) -> Int {
46+
switch char {
47+
case "0" : return 0
48+
case "1" : return 1
49+
case "2" : return 2
50+
case "3" : return 3
51+
case "4" : return 4
52+
case "5" : return 5
53+
case "6" : return 6
54+
case "7" : return 7
55+
case "8" : return 8
56+
case "9" : return 9
57+
case "A", "a", "\u{FF21}", "\u{FF41}" : return 10
58+
case "B", "b", "\u{FF22}", "\u{FF42}" : return 11
59+
case "C", "c", "\u{FF23}", "\u{FF43}" : return 12
60+
case "D", "d", "\u{FF24}", "\u{FF44}" : return 13
61+
case "E", "e", "\u{FF25}", "\u{FF45}" : return 14
62+
case "F", "f", "\u{FF26}", "\u{FF46}" : return 15
63+
case "G", "g", "\u{FF27}", "\u{FF47}" : return 16
64+
case "H", "h", "\u{FF28}", "\u{FF48}" : return 17
65+
case "I", "i", "\u{FF29}", "\u{FF49}" : return 18
66+
case "J", "j", "\u{FF2A}", "\u{FF4A}" : return 19
67+
case "K", "k", "\u{FF2B}", "\u{FF4B}" : return 20
68+
case "L", "l", "\u{FF2C}", "\u{FF4C}" : return 21
69+
case "M", "m", "\u{FF2D}", "\u{FF4D}" : return 22
70+
case "N", "n", "\u{FF2E}", "\u{FF4E}" : return 23
71+
case "O", "o", "\u{FF2F}", "\u{FF4F}" : return 24
72+
case "P", "p", "\u{FF30}", "\u{FF50}" : return 25
73+
case "Q", "q", "\u{FF31}", "\u{FF51}" : return 26
74+
case "R", "r", "\u{FF32}", "\u{FF52}" : return 27
75+
case "S", "s", "\u{FF33}", "\u{FF53}" : return 28
76+
case "T", "t", "\u{FF34}", "\u{FF54}" : return 29
77+
case "U", "u", "\u{FF35}", "\u{FF55}" : return 30
78+
case "V", "v", "\u{FF36}", "\u{FF56}" : return 31
79+
case "W", "w", "\u{FF37}", "\u{FF57}" : return 32
80+
case "X", "x", "\u{FF38}", "\u{FF58}" : return 33
81+
case "Y", "y", "\u{FF39}", "\u{FF59}" : return 34
82+
case "Z", "z", "\u{FF3A}", "\u{FF5A}" : return 35
83+
// fractions
84+
case "\u{00BC}", "\u{00BD}", "\u{00BE}" : return -2
85+
case "\u{2150}", "\u{2151}", "\u{2152}",
86+
"\u{2153}", "\u{2154}", "\u{2155}",
87+
"\u{2156}", "\u{2157}", "\u{2158}",
88+
"\u{2159}", "\u{215A}", "\u{215B}",
89+
"\u{215C}", "\u{215D}", "\u{215E}" : return -2
90+
case "\u{215F}" : return 1
91+
case "\u{2189}" : return 0
92+
default:
93+
if let number = char.wholeNumberValue {
94+
return number
95+
}
96+
return -1
97+
}
98+
}
4099
}

Tests/JavApiTests/JavApi_lang_Character_Tests.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,17 @@ final class JavApi_lang_Character_Tests: XCTestCase {
2828

2929
let aValue = 65
3030
XCTAssertTrue(Character.isLetter(aValue))
31-
31+
}
32+
33+
func testGetNumericValue () {
34+
let A : Character = "A"
35+
XCTAssertTrue(10 == Character.getNumericValue(A))
36+
37+
let _1fract : Character = "\u{215f}"
38+
XCTAssertTrue(1 == Character.getNumericValue(_1fract))
39+
40+
let _1fract4 : Character = "\u{00BC}"
41+
XCTAssertTrue(-2 == Character.getNumericValue(_1fract4))
3242
}
3343

3444
}

0 commit comments

Comments
 (0)