2016-12-30 43 views
1

我正在研究String解析器,其中输入可以有各种格式,而且我事先不知道正在使用哪种格式,所以我需要编写一些灵活的东西。不同格式的Swift解析字符串

的第一步是检查的前几个字符,我可以通过使用如检查:

func parse(input: String) -> String { 

    let result: String 

    if (input.hasPrefix("foo") { 
    result = doFoo(input) 
    } 
    else if (input.hasPrefix("bar") { 
    result = doBar(input) 
    } 
    else if (input.hasPrefix("baz") { 
    result = doBaz(input) 
    } 
    else { 
    result = doBasic(input) 
    } 

    return result 
} 

doXXX()功能都有它自己的解析代码,而这又可以有多种选择,如不同分隔符等

这可能很容易变成很多的if-else代码,我想知道如果用Swift有一个更简单的方法来做到这一点。也许使用switch-case语句,或者其他的东西?我可以使用枚举吗?

编辑:代码是在String扩展。

回答

2

这是我会怎么做:

// This pattern matching operator defines what it means to have a 
// closure as a pattern. If the closure evaluates to true when called 
// with `value` as an arg, then the `pattern` matches the `value`. 
func ~=<T>(pattern: (T) -> Bool, value: T) -> Bool { 
    return pattern(value) 
} 

// This type alias is just here to make the next line a bit more readable. 
// A `BoolInstanceMethod<T, U>` is a closure type that represents an unapplied 
// instance method that ultimately returns a Bool. 

// For example, `String.hasPrefix` has type `(String) -> (String) -> Bool`. 
// The first argument, of type `T` (String, in this case) is the instance 
// this method will be called on. 

// Say we call this: String.hasPrefix("The quick brown fox"). 
// The result has type `(String) -> Bool`. 
// It's equivalent to "The quick brown fox".hasPrefix. 

// We then call the resulting closure with the arguement to hasPrefix 
// For example: String.hasPrefix("The quick brown fox")("The") 
// This has type `Bool`. It's the same as: "The quick brown fox".hasPrefix("The) 
typealias BoolInstanceMethod<T, U> = (_ instance: T) -> (_ arg: U) -> Bool 

// This function wraps a given instance method, in such a way as to reverse the 
// order of the curried arguements. The given instance method is usually called as: 
// Type.instanceMethod(instance)(arg), but this function allows you to swap it, to 
// call it as: apply(Type.instanceMethod)(arg)(instance) 
func apply<T, U>(instanceMethod: @escaping BoolInstanceMethod<T, U>) -> (_ arg: U) -> (_ instance: T) -> Bool { 
    return { arg in 
     return { instance in 
      return instanceMethod(instance)(arg) 
     } 
    } 
} 

// Dummy functions to satisfy the compiler 
func doFoo(_: String) -> String { return "" } 
func doBar(_: String) -> String { return "" } 
func doBaz(_: String) -> String { return "" } 
func doBasic(_: String) -> String { return "" } 

func parse(input: String) -> String { 
    let result: String 

    // The predicate of choice is made, in this case, String.hasPrefix. 
    let hasPrefix = apply(instanceMethod: String.hasPrefix) 

    // The switch calls `~=` for every case, giving it hasPrefix(...) and "input" 
    // as args. The first case that makes `~=` yield `true` is executed. 
    switch input { 
    case hasPrefix("foo"): result = doFoo(input) 
    case hasPrefix("bar"): result = doBar(input) 
    case hasPrefix("baz"): result = doBaz(input) 
    default: result = doBasic(input) 
    } 

    return result 
} 

// You could also implement parse like this: 
func parse2(input: String) -> String { 
    // You can save repeated application of the `input` parameter by doing it 
    // just once at the end (see the `return` of this func). 
    let action: (String) -> String 

    // The predicate of choice is made, in this case, String.hasPrefix. 
    let hasPrefix = apply(instanceMethod: String.hasPrefix) 

    // The switch calls `~=` for every case, giving it hasPrefix(...) and "input" 
    // as args. The first case that makes `~=` yield `true` is executed. 

    switch input { 
    case hasPrefix("foo"): action = doFoo 
    case hasPrefix("bar"): action = doBar 
    case hasPrefix("baz"): action = doBaz 
    default: action = doBasic 
    } 

    return action(input) 
} 
+0

这看起来很有希望。不幸的是,我得到了第一个'func'的编译器错误:'成员操作符'〜='必须至少有一个'String'类型的参数;'我还必须将'static'关键字添加到'func'中。我有一个String扩展的解析代码,可能会导致错误? – Koen

+1

我故意做了'〜='运算符通用,所以它适用于任何类型。在这种情况下,它必须是独立的功能。如果你真的想把它保留在一个'String'扩展中,那么你可以专注于'String'而不是通用的'T',但我建议不要这样做。 – Alexander

+0

我改变了,现在它工作(我正在使用'parse2')。 – Koen