Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

target 'SwiftString_Example', :exclusive => true do
target 'SwiftString_Example' do
pod 'SwiftString', :path => '../'
end

target 'SwiftString_Tests', :exclusive => true do
target 'SwiftString_Tests' do
pod 'SwiftString', :path => '../'
pod 'SwiftHamcrest'
end
28 changes: 15 additions & 13 deletions Pod/Classes/String+HTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public extension String {
// decodeNumeric("20ac", 16) --> "€"
private func decodeNumeric(string : String, base : Int32) -> Character? {
let code = UInt32(strtoul(string, nil, base))
return Character(UnicodeScalar(code))
return Character(UnicodeScalar(code)!)
}

// Decode the HTML character entity to the corresponding
Expand All @@ -281,9 +281,11 @@ public extension String {
// decode("&foo;") --> nil
private func decode(entity : String) -> Character? {
if entity.hasPrefix("&#x") || entity.hasPrefix("&#X"){
return decodeNumeric(entity.substringFromIndex(entity.startIndex.advancedBy(3)), base: 16)
let index = entity.index(entity.startIndex, offsetBy: 3)
return decodeNumeric(string: entity.substring(from: index), base: 16)
} else if entity.hasPrefix("&#") {
return decodeNumeric(entity.substringFromIndex(entity.startIndex.advancedBy(2)), base: 10)
let index = entity.index(entity.startIndex, offsetBy: 2)
return decodeNumeric(string: entity.substring(from: index), base: 10)
} else {
return HTMLEntities.characterEntities[entity]
}
Expand All @@ -298,29 +300,29 @@ public extension String {
var position = startIndex

// Find the next '&' and copy the characters preceding it to `result`:
while let ampRange = self.rangeOfString("&", range: position ..< endIndex) {
result.appendContentsOf(self[position ..< ampRange.startIndex])
position = ampRange.startIndex
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.rangeOfString(";", range: position ..< endIndex) {
let entity = self[position ..< semiRange.endIndex]
position = semiRange.endIndex
if let semiRange = self.range(of: ";", range: position ..< endIndex) {
let entity = self[position ..< semiRange.upperBound]
position = semiRange.upperBound

if let decoded = decode(entity) {
if let decoded = decode(entity: entity) {
// Replace by decoded character:
result.append(decoded)
} else {
// Invalid entity, copy verbatim:
result.appendContentsOf(entity)
result.append(entity)
}
} else {
// No matching ';'.
break
}
}
// Copy remaining characters to `result`:
result.appendContentsOf(self[position ..< endIndex])
result.append(self[position ..< endIndex])
return result
}
}
}
Loading