Skip to content

Commit

Permalink
add Character.getNumericValue (char)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastie committed Jul 4, 2024
1 parent 4553daf commit 9d80bd3
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
33 changes: 33 additions & 0 deletions Sources/JavApi/JavApi⁴Swift.docc/Java2Swift.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ The translation has a Optional problem because Java reference types are implicit

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

##### toString() method

If special toString() implemetation is in Java, first let your Swift type be conform to the *CustomStringConvertible*. Next implement the property *description*. For example

```swift
public func toString () -> String {
return "My name is Bond, James Bond"
}

public var description: String {
get {
return self.toString()
}
}
```

#### nested classes

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

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

If you use enum values in switch, you need a dot before the value name in case.

```java
switch (enumType){
case ENUM_NAME: break;
default: break;
}
```


```swift
switch (enumType){
case .ENUM_NAME: break;
default: break;
}
```

#### visibility

Swift visiblilities are:
Expand Down
61 changes: 60 additions & 1 deletion Sources/JavApi/lang/Character+Java.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,67 @@ extension Character {
asString = String(String.UnicodeScalarView(asArray))
return isLetter(asString.toCharArray()[0])
}

public static func isWhitespace (_ char : char) -> boolean {
return char.isWhitespace
}

/// Return the numeric value of Unicode character, f.e. ``\u{216D}`` returns 100.
///
/// - Parameters: char
/// - Returns: numeric value or 10 to 35 for A-Z or -2 for fractions or -1 for all others
public static func getNumericValue (_ char : Character) -> Int {
switch char {
case "0" : return 0
case "1" : return 1
case "2" : return 2
case "3" : return 3
case "4" : return 4
case "5" : return 5
case "6" : return 6
case "7" : return 7
case "8" : return 8
case "9" : return 9
case "A", "a", "\u{FF21}", "\u{FF41}" : return 10
case "B", "b", "\u{FF22}", "\u{FF42}" : return 11
case "C", "c", "\u{FF23}", "\u{FF43}" : return 12
case "D", "d", "\u{FF24}", "\u{FF44}" : return 13
case "E", "e", "\u{FF25}", "\u{FF45}" : return 14
case "F", "f", "\u{FF26}", "\u{FF46}" : return 15
case "G", "g", "\u{FF27}", "\u{FF47}" : return 16
case "H", "h", "\u{FF28}", "\u{FF48}" : return 17
case "I", "i", "\u{FF29}", "\u{FF49}" : return 18
case "J", "j", "\u{FF2A}", "\u{FF4A}" : return 19
case "K", "k", "\u{FF2B}", "\u{FF4B}" : return 20
case "L", "l", "\u{FF2C}", "\u{FF4C}" : return 21
case "M", "m", "\u{FF2D}", "\u{FF4D}" : return 22
case "N", "n", "\u{FF2E}", "\u{FF4E}" : return 23
case "O", "o", "\u{FF2F}", "\u{FF4F}" : return 24
case "P", "p", "\u{FF30}", "\u{FF50}" : return 25
case "Q", "q", "\u{FF31}", "\u{FF51}" : return 26
case "R", "r", "\u{FF32}", "\u{FF52}" : return 27
case "S", "s", "\u{FF33}", "\u{FF53}" : return 28
case "T", "t", "\u{FF34}", "\u{FF54}" : return 29
case "U", "u", "\u{FF35}", "\u{FF55}" : return 30
case "V", "v", "\u{FF36}", "\u{FF56}" : return 31
case "W", "w", "\u{FF37}", "\u{FF57}" : return 32
case "X", "x", "\u{FF38}", "\u{FF58}" : return 33
case "Y", "y", "\u{FF39}", "\u{FF59}" : return 34
case "Z", "z", "\u{FF3A}", "\u{FF5A}" : return 35
// fractions
case "\u{00BC}", "\u{00BD}", "\u{00BE}" : return -2
case "\u{2150}", "\u{2151}", "\u{2152}",
"\u{2153}", "\u{2154}", "\u{2155}",
"\u{2156}", "\u{2157}", "\u{2158}",
"\u{2159}", "\u{215A}", "\u{215B}",
"\u{215C}", "\u{215D}", "\u{215E}" : return -2
case "\u{215F}" : return 1
case "\u{2189}" : return 0
default:
if let number = char.wholeNumberValue {
return number
}
return -1
}
}
}
12 changes: 11 additions & 1 deletion Tests/JavApiTests/JavApi_lang_Character_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ final class JavApi_lang_Character_Tests: XCTestCase {

let aValue = 65
XCTAssertTrue(Character.isLetter(aValue))

}

func testGetNumericValue () {
let A : Character = "A"
XCTAssertTrue(10 == Character.getNumericValue(A))

let _1fract : Character = "\u{215f}"
XCTAssertTrue(1 == Character.getNumericValue(_1fract))

let _1fract4 : Character = "\u{00BC}"
XCTAssertTrue(-2 == Character.getNumericValue(_1fract4))
}

}

0 comments on commit 9d80bd3

Please sign in to comment.