forked from jasder/antlr
Remove unused functions from StringExtension.
Some of these are subject to change for Swift 4, but since they are unused we should remove them anyway.
This commit is contained in:
parent
8ba2c0ba4f
commit
4bafc74e6c
|
@ -11,14 +11,6 @@ import Foundation
|
|||
|
||||
extension String {
|
||||
|
||||
func split(_ separator: String) -> [String] {
|
||||
return self.components(separatedBy: separator)
|
||||
}
|
||||
|
||||
func containsIgnoreCase(_ find: String) -> Bool {
|
||||
return self.lowercased().range(of: find.lowercased()) != nil
|
||||
}
|
||||
|
||||
var length: Int {
|
||||
return self.characters.count
|
||||
}
|
||||
|
@ -60,32 +52,6 @@ extension String {
|
|||
return index
|
||||
}
|
||||
|
||||
func substringAfter(_ string: String) -> String {
|
||||
if let range = self.range(of: string) {
|
||||
let intIndex: Int = self.characters.distance(from: self.startIndex, to: range.upperBound)
|
||||
return self.substring(from: self.characters.index(self.startIndex, offsetBy: intIndex))
|
||||
}
|
||||
return self
|
||||
|
||||
}
|
||||
|
||||
var lowercaseFirstChar: String {
|
||||
var result = self
|
||||
if self.length > 0 {
|
||||
let startIndex = self.startIndex
|
||||
result.replaceSubrange(startIndex ... startIndex, with: String(self[startIndex]).lowercased())
|
||||
}
|
||||
return result
|
||||
}
|
||||
func substringWithRange(_ range: Range<Int>) -> String {
|
||||
|
||||
|
||||
let start = self.characters.index(self.startIndex, offsetBy: range.lowerBound)
|
||||
|
||||
let end = self.characters.index(self.startIndex, offsetBy: range.upperBound)
|
||||
return self.substring(with: start ..< end)
|
||||
}
|
||||
|
||||
subscript(integerIndex: Int) -> Character {
|
||||
let index = characters.index(startIndex, offsetBy: integerIndex)
|
||||
return self[index]
|
||||
|
@ -97,116 +63,5 @@ extension String {
|
|||
let range = start ..< end
|
||||
return self[range]
|
||||
}
|
||||
|
||||
func charAt(_ index: Int) -> Character {
|
||||
return self[self.characters.index(self.startIndex, offsetBy: index)]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Mapping from XML/HTML character entity reference to character
|
||||
// From http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
|
||||
private let characterEntities: [String:Character] = [
|
||||
// XML predefined entities:
|
||||
""": "\"",
|
||||
"&": "&",
|
||||
"'": "'",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
|
||||
// HTML character entity references:
|
||||
" ": "\u{00a0}",
|
||||
// ...
|
||||
"♦": "♦",
|
||||
]
|
||||
|
||||
extension String {
|
||||
|
||||
///
|
||||
/// Returns a new string made by replacing in the `String`
|
||||
/// all HTML character entity references with the corresponding
|
||||
/// character.
|
||||
///
|
||||
var stringByDecodingHTMLEntities: String {
|
||||
|
||||
|
||||
// Convert the number in the string to the corresponding
|
||||
// Unicode character, e.g.
|
||||
// decodeNumeric("64", 10) --> "@"
|
||||
// decodeNumeric("20ac", 16) --> "€"
|
||||
func decodeNumeric(_ string: String, base: Int32) -> Character? {
|
||||
let code = UInt32(strtoul(string, nil, base))
|
||||
return Character(UnicodeScalar(code)!)
|
||||
}
|
||||
|
||||
// Decode the HTML character entity to the corresponding
|
||||
// Unicode character, return `nil` for invalid input.
|
||||
// decode("@") --> "@"
|
||||
// decode("€") --> "€"
|
||||
// decode("<") --> "<"
|
||||
// decode("&foo;") --> nil
|
||||
func decode(_ entity: String) -> Character? {
|
||||
|
||||
if entity.hasPrefix("&#x") || entity.hasPrefix("&#X") {
|
||||
return decodeNumeric(entity.substring(from: entity.characters.index(entity.startIndex, offsetBy: 3)), base: 16)
|
||||
} else if entity.hasPrefix("&#") {
|
||||
return decodeNumeric(entity.substring(from: entity.characters.index(entity.startIndex, offsetBy: 2)), base: 10)
|
||||
} else {
|
||||
return characterEntities[entity]
|
||||
}
|
||||
}
|
||||
|
||||
var result = ""
|
||||
var position = startIndex
|
||||
|
||||
// Find the next '&' and copy the characters preceding it to `result`:
|
||||
while let ampRange = self.range(of: "&", range: position ..< endIndex) {
|
||||
result.append(self[position ..< ampRange.lowerBound])
|
||||
position = ampRange.lowerBound
|
||||
|
||||
// Find the next ';' and copy everything from '&' to ';' into `entity`
|
||||
if let semiRange = self.range(of: ";", range: position ..< endIndex) {
|
||||
let entity = self[position ..< semiRange.upperBound]
|
||||
position = semiRange.upperBound
|
||||
|
||||
if let decoded = decode(entity) {
|
||||
// Replace by decoded character:
|
||||
result.append(decoded)
|
||||
} else {
|
||||
// Invalid entity, copy verbatim:
|
||||
result.append(entity)
|
||||
}
|
||||
} else {
|
||||
// No matching ';'.
|
||||
break
|
||||
}
|
||||
}
|
||||
// Copy remaining characters to `result`:
|
||||
result.append(self[position ..< endIndex])
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
|
||||
static let htmlEscapedDictionary = [
|
||||
"&": "&",
|
||||
""": "\"",
|
||||
"'": "'",
|
||||
"9": "'",
|
||||
"’": "'",
|
||||
"–": "'",
|
||||
">": ">",
|
||||
"<": "<"
|
||||
]
|
||||
|
||||
public var escapedHtmlString: String {
|
||||
var newString = "\(self)"
|
||||
|
||||
for (key, value) in String.htmlEscapedDictionary {
|
||||
newString = newString.replacingOccurrences(of: value, with: key)
|
||||
}
|
||||
return newString
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -320,7 +320,6 @@ public class ParseTreePatternMatcher {
|
|||
// add special rule token or conjure up new token from name
|
||||
let firstStr = String(tagChunk.getTag()[0])
|
||||
if firstStr.lowercased() != firstStr {
|
||||
//if ( Character.isUpperCase(tagChunk.getTag().charAt(0)) ) {
|
||||
let ttype: Int = parser.getTokenType(tagChunk.getTag())
|
||||
if ttype == CommonToken.INVALID_TYPE {
|
||||
throw ANTLRError.illegalArgument(msg: "Unknown token " + tagChunk.getTag() + " in pattern: " + pattern)
|
||||
|
@ -329,7 +328,6 @@ public class ParseTreePatternMatcher {
|
|||
tokens.append(t)
|
||||
} else {
|
||||
if firstStr.uppercased() != firstStr {
|
||||
// if ( Character.isLowerCase(tagChunk.getTag().charAt(0)) ) {
|
||||
let ruleIndex: Int = parser.getRuleIndex(tagChunk.getTag())
|
||||
if ruleIndex == -1 {
|
||||
throw ANTLRError.illegalArgument(msg: "Unknown rule " + tagChunk.getTag() + " in pattern: " + pattern)
|
||||
|
|
Loading…
Reference in New Issue