Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from heidipuk/update-to-vapor3
Browse files Browse the repository at this point in the history
Update to vapor3
  • Loading branch information
BrettRToomey committed Dec 10, 2018
2 parents 0916cd7 + ffef8f8 commit 5d9627c
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 41 deletions.
11 changes: 6 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ version: 2
jobs:
MacOS:
macos:
xcode: "9.0"
xcode: "10.0"
steps:
- checkout
- restore_cache:
keys:
- v1-spm-deps-{{ checksum "Package.swift" }}
- run:
name: Install CMySQL and CTLS
name: Install dependencies
command: |
brew tap vapor/homebrew-tap
brew install cmysql
brew install ctls
brew install libressl
- run:
name: Build and Run Tests
no_output_timeout: 1800
Expand All @@ -30,22 +31,22 @@ jobs:
- .build
Linux:
docker:
- image: brettrtoomey/vapor-ci:0.0.1
- image: nodesvapor/vapor-ci:swift-4.2
steps:
- checkout
- restore_cache:
keys:
- v2-spm-deps-{{ checksum "Package.swift" }}
- run:
name: Copy Package file
name: Copy Package File
command: cp Package.swift res
- run:
name: Build and Run Tests
no_output_timeout: 1800
command: |
swift test -Xswiftc -DNOJSON
- run:
name: Restoring Package file
name: Restoring Package File
command: mv res Package.swift
- save_cache:
key: v2-spm-deps-{{ checksum "Package.swift" }}
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ xcuserdata/
## Other
*.moved-aside
*.xcuserstate
test.Dockerfile
dockerTest.sh

## Obj-C/Swift specific
*.hmap
Expand All @@ -39,6 +41,7 @@ playground.xcworkspace
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
.build/
Package.resolved

# CocoaPods
#
Expand Down
17 changes: 16 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
// swift-tools-version:4.2

import PackageDescription

let package = Package(
name: "DataURI",
products: [
.library(name: "DataURI", targets: ["DataURI"])
],
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2),
.package(url: "https://github.com/vapor/core.git", from: "3.0.0"),
],
targets: [
.target(
name: "DataURI",
dependencies: ["Core"]
),
.testTarget(
name: "DataURITests",
dependencies: ["DataURI"]
)
]
)
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# DataURI
[![Swift Version](https://img.shields.io/badge/Swift-3-brightgreen.svg)](http://swift.org)
[![Vapor Version](https://img.shields.io/badge/Vapor-2-F6CBCA.svg)](http://vapor.codes)
[![Swift Version](https://img.shields.io/badge/Swift-4.2-brightgreen.svg)](http://swift.org)
[![Vapor Version](https://img.shields.io/badge/Vapor-3-30B6FC.svg)](http://vapor.codes)
[![Circle CI](https://circleci.com/gh/nodes-vapor/data-uri/tree/master.svg?style=shield)](https://circleci.com/gh/nodes-vapor/data-uri)
[![codebeat badge](https://codebeat.co/badges/7f0cab4f-f11b-43d5-8484-bc9300c23d81)](https://codebeat.co/projects/github-com-nodes-vapor-data-uri-master)
[![codecov](https://codecov.io/gh/nodes-vapor/data-uri/branch/master/graph/badge.svg)](https://codecov.io/gh/nodes-vapor/data-uri)
Expand All @@ -14,7 +14,7 @@ A pure Swift parser for Data URIs.

Update your `Package.swift` file.
```swift
.Package(url: "https://github.com/nodes-vapor/data-uri.git", majorVersion: 1)
.package(url: "https://github.com/nodes-vapor/data-uri.git", from: "2.0.0")
```


Expand Down
14 changes: 14 additions & 0 deletions Sources/DataURI/Byte+asciiCode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public typealias Byte = UInt8
public typealias Bytes = [Byte]

extension Byte {
internal var asciiCode: Byte {
if self >= 48 && self <= 57 {
return self - 48
} else if self >= 65 && self <= 70 {
return self - 55
} else {
return 0
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Core
import Foundation

extension String {
Expand All @@ -11,4 +10,9 @@ extension String {
let (data, type, _) = try DataURIParser.parse(uri: self)
return (data, type.makeString())
}

/// Converts the string to a UTF8 array of bytes.
public var bytes: [UInt8] {
return [UInt8](self.utf8)
}
}
22 changes: 6 additions & 16 deletions Sources/Parser.swift → Sources/DataURI/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public struct DataURIParser {
case invalidScheme
case invalidURI
}

var scanner: Scanner<Byte>

init(scanner: Scanner<Byte>) {
Expand Down Expand Up @@ -41,10 +40,13 @@ extension DataURIParser {
type = "text/plain;charset=US-ASCII".bytes
}

if let typeMetadata = typeMetadata, typeMetadata == "base64".bytes {
data = data.base64Decoded
if typeMetadata == "base64".bytes,
let decodedData = Data(base64Encoded: data.convertToData()),
let dataString = String(data: decodedData, encoding: .utf8)
{
data = dataString.bytes
}

return (data, type, typeMetadata)
}
}
Expand Down Expand Up @@ -150,15 +152,3 @@ extension DataURIParser {
return (leftMostDigit.asciiCode * 16) + rightMostDigit.asciiCode
}
}

extension Byte {
internal var asciiCode: Byte {
if self >= 48 && self <= 57 {
return self - 48
} else if self >= 65 && self <= 70 {
return self - 55
} else {
return 0
}
}
}
File renamed without changes.
12 changes: 12 additions & 0 deletions Sources/DataURI/Sequence+makeString.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extension Sequence where Iterator.Element == Byte {
/// Converts a slice of bytes to
/// string. Courtesy of Vapor 2 - Core and @vzsg
public func makeString() -> String {
let array = Array(self) + [0]

return array.withUnsafeBytes { rawBuffer in
guard let pointer = rawBuffer.baseAddress?.assumingMemoryBound(to: CChar.self) else { return nil }
return String(validatingUTF8: pointer)
} ?? ""
}
}
7 changes: 0 additions & 7 deletions Tests/DataURITests/DataURITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ class DataURITests: XCTestCase {
("testSpeed", testSpeed)
]

func testBase64() {
//FIXME(Brett): remove when vapor/core is updated to 1.1
let base64Bytes: Bytes = "SGVsbG8sIHdvcmxkIQ==".makeBytes()
let output: Bytes = base64Bytes.base64URLDecoded
XCTAssertEqual(output.makeString(), "Hello, world!")
}

func testTextNoType() {
let (data, type, meta) = try! DataURIParser.parse(
uri: "data:,Hello%2C%20World!"
Expand Down
6 changes: 3 additions & 3 deletions Tests/DataURITests/Utilities/Expect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ func expect<E: Error, ReturnType>(
toThrow expectedError: E,
file: StaticString = #file,
line: UInt = #line,
from closure: (Void) throws -> ReturnType
from closure: () throws -> ReturnType
) where E: Equatable {
do {
let _ = try closure()
Expand All @@ -23,7 +23,7 @@ func expect<E: Error, ReturnType>(
func expectNoThrow<ReturnType>(
file: StaticString = #file,
line: UInt = #line,
_ closure: (Void) throws -> ReturnType
_ closure: () throws -> ReturnType
) {
do {
let _ = try closure()
Expand All @@ -33,7 +33,7 @@ func expectNoThrow<ReturnType>(
}

func expect<ReturnType>(
_ closure: (Void) throws -> ReturnType,
_ closure: () throws -> ReturnType,
file: StaticString = #file,
line: UInt = #line,
toReturn expectedResult: ReturnType
Expand Down
37 changes: 37 additions & 0 deletions Tests/DataURITests/XCTestManifests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import XCTest

extension DataURITests {
static let __allTests = [
("testBase64Text", testBase64Text),
("testFailedInvalidScheme", testFailedInvalidScheme),
("testHTMLJavascriptText", testHTMLJavascriptText),
("testHTMLText", testHTMLText),
("testPublicInterface", testPublicInterface),
("testSpeed", testSpeed),
("testTextNoType", testTextNoType),
]
}

extension ParserTests {
static let __allTests = [
("testConsume", testConsume),
("testConsumePercentDecoded", testConsumePercentDecoded),
("testConsumePercentDecodedFailed", testConsumePercentDecodedFailed),
("testConsumeUntil", testConsumeUntil),
("testConsumeWhile", testConsumeWhile),
("testExtractType", testExtractType),
("testExtractTypeFailed", testExtractTypeFailed),
("testExtractTypeMetadata", testExtractTypeMetadata),
("testExtractTypeWithMetadata", testExtractTypeWithMetadata),
("testParserInit", testParserInit),
]
}

#if !os(macOS)
public func __allTests() -> [XCTestCaseEntry] {
return [
testCase(DataURITests.__allTests),
testCase(ParserTests.__allTests),
]
}
#endif
11 changes: 6 additions & 5 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import XCTest
@testable import DataURITests

XCTMain([
testCase(DataURITests.allTests),
testCase(ParserTests.allTests),
])
import DataURITests

var tests = [XCTestCaseEntry]()
tests += DataURITests.__allTests()

XCTMain(tests)

0 comments on commit 5d9627c

Please sign in to comment.