Skip to content

Commit

Permalink
Merge pull request #103 from angelolloqui/feature/unicode
Browse files Browse the repository at this point in the history
Feature/unicode
  • Loading branch information
angelolloqui authored Nov 8, 2019
2 parents ec5a7f5 + 7f480cf commit 2248ab9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions Assets/Tests/KotlinTokenizer/string_interpolator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ var string2 = "full name: ${name} ${lastName}"
var string3 = "name: ${name ?: lastName}"
var string4 = "name: ${user?.name ?: ("-" + lastName)}"
var string5 = "name: ${list.map({ it.name }) ?: "empty"} \tactive: ${active}"
var unicode = "A special \u016C char and interpolator ${name}"
1 change: 1 addition & 0 deletions Assets/Tests/KotlinTokenizer/string_interpolator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ var string2 = "full name: \(name) \(lastName)"
var string3 = "name: \(name ?? lastName)"
var string4 = "name: \(user?.name ?? ("-" + lastName))"
var string5 = "name: \(list.map({ $0.name }) ?? "empty") \tactive: \(active)"
var unicode = "A special \u{016C} char and interpolator \(name)"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,4 @@ And special credits for the contributors:
- [Tomohiro Matsuzawa](https://github.com/thmatuza)
- [Tor Langballe](https://github.com/torlangballe)
- [Levi Dahlstrom](https://github.com/LeviDahl)
- [Shaurya](https://github.com/shaurya-ObjC)
31 changes: 30 additions & 1 deletion Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,8 @@ public class KotlinTokenizer: SwiftTokenizer {
return [expression.newToken(.keyword, "null")]
case let .interpolatedString(_, rawText):
return tokenizeInterpolatedString(rawText, node: expression)
case let .staticString(_, rawText):
return [expression.newToken(.string, conversionUnicodeString(rawText, node: expression))]
case .array(let exprs):
let isGenericTypeInfo = expression.lexicalParent is FunctionCallExpression
return
Expand Down Expand Up @@ -1012,8 +1014,35 @@ public class KotlinTokenizer: SwiftTokenizer {
].joined(token: node.newToken(.linebreak, "\n"))
}

private func tokenizeInterpolatedString(_ rawText: String, node: ASTNode) -> [Token] {
private func conversionUnicodeString(_ rawText:String, node:ASTNode) -> String {
var remainingText = rawText
var unicodeString = ""

while let startRange = remainingText.range(of: "u{") {
unicodeString += remainingText[..<startRange.lowerBound] + "u"
remainingText = String(remainingText[startRange.upperBound...])

var scopes = 1
var i = 1
while i < remainingText.count && scopes > 0 {
let index = remainingText.index(remainingText.startIndex, offsetBy: i)
i += 1
switch remainingText[index] {
case "}": scopes -= 1
default: continue
}
}

unicodeString += remainingText[..<remainingText.index(remainingText.startIndex, offsetBy: i - 1)]
remainingText = String(remainingText[remainingText.index(remainingText.startIndex, offsetBy: i)...])
}

unicodeString += remainingText
return unicodeString
}

private func tokenizeInterpolatedString(_ rawText: String, node: ASTNode) -> [Token] {
var remainingText = conversionUnicodeString(rawText, node: node)
var interpolatedString = ""

while let startRange = remainingText.range(of: "\\(") {
Expand Down

0 comments on commit 2248ab9

Please sign in to comment.