//
//  String+Misc.swift
//  Swifter
//
//  Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
//

import Foundation

extension String {

    public func unquote() -> String {
        var scalars = self.unicodeScalars
        if scalars.first == "\"" && scalars.last == "\"" && scalars.count >= 2 {
            scalars.removeFirst()
            scalars.removeLast()
            return String(scalars)
        }
        return self
    }
    
    public func appendingPathComponent(_ path: String) -> String {
        return (self as NSString).appendingPathComponent(path) as String
    }

    func regex(text: String) -> Bool {
        if let regex = try? NSRegularExpression(pattern: self, options: NSRegularExpression.Options.caseInsensitive) {
            let res = regex.matches(in: text, options: .anchored, range: NSRange(location: 0, length: text.count))
            return res.count > 0
        }
        return false
    }
}

extension UnicodeScalar {

    public func asWhitespace() -> UInt8? {
        if self.value >= 9 && self.value <= 13 {
            return UInt8(self.value)
        }
        if self.value == 32 {
            return UInt8(self.value)
        }
        return nil
    }
}
