Skip to content

Commit

Permalink
simplified smartJoin
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka committed Dec 29, 2017
1 parent d1507d3 commit 8ffc3a1
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions Sources/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ extension String {
var singleQuoteCount = 0
var doubleQuoteCount = 0

let specialCharacters = ",|:"
func appendWord(_ word: String) {
if components.count > 0 {
if specialCharacters.contains(word) ||
specialCharacters.characters.contains(components.last!.characters.last!) {
components[components.count - 1] += word
} else {
components.append(word)
}
} else {
components.append(word)
}
}

for character in self.characters {
if character == "'" { singleQuoteCount += 1 }
else if character == "\"" { doubleQuoteCount += 1 }
Expand All @@ -19,7 +33,7 @@ extension String {
if separate != separator {
word.append(separate)
} else if singleQuoteCount % 2 == 0 && doubleQuoteCount % 2 == 0 && !word.isEmpty {
components.append(word)
appendWord(word)
word = ""
}

Expand All @@ -33,38 +47,11 @@ extension String {
}

if !word.isEmpty {
components.append(word)
appendWord(word)
}

return smartJoin(components)
}
}

// joins back components around characters used in variables lists and filters
private func smartJoin(_ components: [String]) -> [String] {
var joinedComponents = components
// convert ["a", "|", "b"] and ["a|", "b"] to ["a|b"]
// do not allow ["a", "|b"]
for char in [",", "|", ":"] {
while let index = joinedComponents.index(of: char) {
if index > 0 {
joinedComponents[index-1] += char

if joinedComponents.count > index + 1 {
joinedComponents[index-1] += joinedComponents[index+1]
joinedComponents.remove(at: index+1)
}
}
joinedComponents.remove(at: index)
}
while let index = joinedComponents.index(where: { $0.hasSuffix(char) }) {
if joinedComponents.count > index {
joinedComponents[index] += joinedComponents[index+1]
joinedComponents.remove(at: index+1)
}
}
return components
}
return joinedComponents
}


Expand Down

0 comments on commit 8ffc3a1

Please sign in to comment.