swift3 CharacterSet.urlxxxx addingPercentEncoding


CharacterSet.url???? 系、どれがどのキャラクタをエンコードするのかドキュメントではわからんから解析メモ

key chars example
urlFragmentAllowed !$&'()*+,-./:;=?@_~ http://google.com?a=b&c=d
urlHostAllowed !$&'()*+,-.:;=[]_~ http:%2F%2Fgoogle.com%3Fa=b&c=d
urlPathAllowed !$&'()*+,-./:=@_~ http://google.com%3Fa=b&c=d
urlQueryAllowed !$&'()*+,-./:;=?@_~ http://google.com?a=b&c=d
urlUserAllowed !$&'()*+,-.;=_~ http%3A%2F%2Fgoogle.com%3Fa=b&c=d
urlPathAllowed !$&'()*+,-./:=@_~ http://google.com%3Fa=b&c=d

code

func cs(cs:CharacterSet)->String {
    var s = ""

    let ar = [0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
              0x3a,0x3b,0x3c,0x3d,0x3e, 0x3f,
              0x40,
              0x5b,0x5c,0x5d,0x5e,0x5f,
              0x7b,0x7c,0x7d,0x7e,0x7f,
              ]

    for i in ar {
        let uc = UnicodeScalar(i)!
        if cs.contains(uc) {
            s += String(uc)
        }
    }

    let url = "http://google.com?a=b&c=d"
    s += "   " +  url.addingPercentEncoding(withAllowedCharacters: cs)!

    return s

}