Compare commits

..

5 Commits

Author SHA1 Message Date
Yuji Ichikawa f87312f66f added extension 2020-02-02 10:34:50 +09:00
Yuji Ichikawa d4fdc4425c made api public 2020-02-02 10:17:11 +09:00
Yuji Ichikawa 186c9e4fe2 made api public 2020-02-02 10:07:41 +09:00
Yuji Ichikawa 470e20d50a made api public 2020-02-02 10:06:13 +09:00
Yuji Ichikawa ed2e8c695b added shallowCopy 2020-02-02 09:51:04 +09:00
4 changed files with 48 additions and 9 deletions

View File

@ -12,7 +12,7 @@ let package = Package(
name: "your-project",
dependencies: [
...
.package(url: "https://github.com/y-ich/swift-DDP.git", .branch("master")),
.package(url: "https://github.com/y-ich/swift-SGF.git", .branch("master")),
],
targets: [
.target(

View File

@ -1,6 +1,6 @@
import Foundation
extension NSTextCheckingResult {
public extension NSTextCheckingResult {
func groups(testedString: String) -> [String] {
return (0 ..< self.numberOfRanges).map {
(testedString as NSString).substring(with: self.range(at: $0))

View File

@ -10,10 +10,10 @@ import Foundation
/// Cursor to traverse SGF collection
open class SGFCursor {
let collection: [SGFNode]
var current: SGFNode?
var history = [SGFNode]()
var moveNumber: Int {
public let collection: [SGFNode]
public var current: SGFNode?
public var history = [SGFNode]()
public var moveNumber: Int {
get {
return history.count
}
@ -98,7 +98,7 @@ open class SGFCursor {
return c
}
func toSgf() -> String {
open func toSgf() -> String {
var root: SGFNode?
if let copied: SGFNode = history.reduce(nil, { parent, node in
let copied = SGFNode()
@ -120,4 +120,11 @@ open class SGFCursor {
}
return SGFEncoder.encode(collection: [root!])
}
open func shallowCopy() -> SGFCursor {
let cursor = SGFCursor(collection)
cursor.current = current
cursor.history = history
return cursor
}
}

View File

@ -62,12 +62,44 @@ public class SGFNode {
}
}
extension SGFNode: CustomDebugStringConvertible {
public var debugDescription: String {
public extension SGFNode: CustomDebugStringConvertible {
var debugDescription: String {
return "\(properties), \(children.count) children"
}
}
public extension SGFNode {
var dimension: (Int, Int)? {
get {
let sz = self["SZ"]?.first ?? ""
let wh = sz.split(separator: ":")
switch wh.count {
case 0:
return (19, 19)
case 1:
if let size = Int(wh[0]) {
return (size, size)
} else {
return nil
}
case 2:
if let w = Int(wh[0]), let h = Int(wh[1]) {
return (w, h)
} else {
return nil
}
default:
return nil
}
}
}
var komi: Float? {
get {
return Float(self["KM"]?.first ?? "")
}
}
}
/**
parses a string in SGF
- Parameter sgf: SGF string