Tidy up some uses of String.characters and integer indexing into Strings.

In Swift 4, Strings have a set of sequence operations that we can use, so
that we don't need our String extensions.  Tidy up a bunch of places where
the code has been converted from Java through Swift 3 and Swift 4, and
become a mess.
This commit is contained in:
Ewan Mellor 2017-11-01 15:38:25 -07:00
parent 775040e186
commit 73d90ec18b
No known key found for this signature in database
GPG Key ID: 7CE1C6BC9EC8645D
7 changed files with 27 additions and 41 deletions

View File

@ -42,8 +42,8 @@ public class ANTLRInputStream: CharStream {
/// Copy data in string to a local char array
///
public init(_ input: String) {
self.data = Array(input.characters) // input.toCharArray();
self.n = input.length
self.data = Array(input)
self.n = data.count
}
///

View File

@ -421,21 +421,19 @@ open class Lexer: Recognizer<LexerATNSimulator>, TokenSource {
}
open func getErrorDisplay(_ c: Character) -> String {
var s = String(c)
if c.integerValue == CommonToken.EOF {
s = "<EOF>"
return "<EOF>"
}
switch s {
switch c {
case "\n":
s = "\\n"
return "\\n"
case "\t":
s = "\\t"
return "\\t"
case "\r":
s = "\\r"
return "\\r"
default:
break
return String(c)
}
return s
}
open func getCharErrorDisplay(_ c: Character) -> String {

View File

@ -142,9 +142,8 @@ public class ListTokenSource: TokenSource {
var line = lastToken.getLine()
if let tokenText = lastToken.getText() {
let length = tokenText.length
for j in 0..<length {
if String(tokenText[j]) == "\n" {
for c in tokenText {
if c == "\n" {
line += 1
}
}

View File

@ -429,7 +429,7 @@ open class Parser: Recognizer<ParserATNSimulator> {
if result == nil {
let deserializationOptions = ATNDeserializationOptions()
try! deserializationOptions.setGenerateRuleBypassTransitions(true)
result = try! ATNDeserializer(deserializationOptions).deserialize(Array(serializedAtn.characters))
result = try! ATNDeserializer(deserializationOptions).deserialize(Array(serializedAtn))
self.bypassAltsAtnCache[serializedAtn] = result!
}
}

View File

@ -85,27 +85,25 @@ public class Vocabulary: Hashable {
/// the display names of tokens.
///
public static func fromTokenNames(_ tokenNames: [String?]?) -> Vocabulary {
guard let tokenNames = tokenNames , tokenNames.count > 0 else {
guard let tokenNames = tokenNames, tokenNames.count > 0 else {
return EMPTY_VOCABULARY
}
var literalNames: [String?] = tokenNames// Arrays.copyOf(tokenNames, tokenNames.length);
var symbolicNames: [String?] = tokenNames
var literalNames = tokenNames
var symbolicNames = tokenNames
let length = tokenNames.count
for i in 0..<length {
guard let tokenName = tokenNames[i] else {
continue
}
if !tokenName.isEmpty {
let firstChar: Character = tokenName[0]
if let firstChar = tokenName.first {
if firstChar == "\'" {
symbolicNames[i] = nil
continue
} else {
if String(firstChar).uppercased() != String(firstChar) {
literalNames[i] = nil
continue
}
}
else if String(firstChar).uppercased() != String(firstChar) {
literalNames[i] = nil
continue
}
}
@ -141,20 +139,17 @@ public class Vocabulary: Hashable {
public func getDisplayName(_ tokenType: Int) -> String {
if tokenType >= 0 && tokenType < displayNames.count {
let displayName: String? = displayNames[tokenType]
if displayName != nil {
return displayName!
if let displayName = displayNames[tokenType] {
return displayName
}
}
let literalName: String? = getLiteralName(tokenType)
if literalName != nil {
return literalName!
if let literalName = getLiteralName(tokenType) {
return literalName
}
let symbolicName: String? = getSymbolicName(tokenType)
if symbolicName != nil {
return symbolicName!
if let symbolicName = getSymbolicName(tokenType) {
return symbolicName
}
return String(tokenType)

View File

@ -613,13 +613,7 @@ public class IntervalSet: IntSet, Hashable, CustomStringConvertible {
public func size() -> Int {
var n = 0
let numIntervals = intervals.count
if numIntervals == 1 {
let firstInterval = intervals[0]
return firstInterval.b - firstInterval.a + 1
}
for i in 0..<numIntervals {
let interval = intervals[i]
for interval in intervals {
n += (interval.b - interval.a + 1)
}
return n

View File

@ -435,7 +435,7 @@ public class ParseTreePatternMatcher {
let c = chunks[i]
if let tc = c as? TextChunk {
let unescaped = tc.getText().replacingOccurrences(of: escape, with: "")
if unescaped.length < tc.getText().length {
if unescaped.count < tc.getText().count {
chunks[i] = TextChunk(unescaped)
}
}