This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacterSet+urlAllowedSets.swift
67 lines (60 loc) · 2.6 KB
/
CharacterSet+urlAllowedSets.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// CharacterSet+urlAllowedSets.swift
// LabsSearch
//
// Created by Xcode on ’19/06/23.
//
import Foundation
extension CharacterSet {
/// A collection of all defined URL-related `CharacterSet`s.
///
/// These sets, along with the `#` (hash) symbol, are available in one set called [.urlAllowedCharacters](x-source-tag://CharacterSet.urlAllowedCharacters).
private static var urlAllowedSets: [CharacterSet] {
return [
.urlUserAllowed,
.urlPasswordAllowed,
.urlHostAllowed,
.urlPathAllowed,
.urlQueryAllowed,
.urlFragmentAllowed
]
}
/// Characters valid in at least one part of a URL.
///
/// These characters are not allowed in *all* parts of a URL; each part has different requirements. This set is useful for checking for Unicode characters that need to be percent encoded before performing a validity check on individual URL components.
///
/// ````
/// let str = "http://www.example.com/~user/index.php?q=test%20query"
/// CharacterSet.urlAllowedCharacters.isSuperset(of: CharacterSet(charactersIn: str))
/// ````
///
/// For a set which contains only characters safe in all parts of a URL, use [.urlSafeCharacters](x-source-tag://CharacterSet.urlSafeCharacters).
///
/// - Tag: CharacterSet.urlAllowedCharacters
static var urlAllowedCharacters: CharacterSet {
// Start by including hash, which isn't in any URL set
// Then include all URL-legal characters
let set = CharacterSet(charactersIn: "#")
return set.union(urlAllowedSets)
}
/// Characters valid in all parts of a URL.
///
/// The characters in this set should not have to be percent encoded no matter which component of a URL in which they appear.
///
/// For a set which contains all possible URL characters, without concern for component restrictions, use [.urlAllowedCharacters](x-source-tag://CharacterSet.urlAllowedCharacters).
///
/// - Tag: CharacterSet.urlSafeCharacters
static var urlSafeCharacters: CharacterSet {
var set = CharacterSet(intersectingCharactersIn: urlAllowedSets)
// Remove ampersand, which is in all URL sets, apparently
set.remove(charactersIn: "&")
return set
}
/// Character set including colon, forward slash, and other undesirables.
static var invalidFileNameCharacters: CharacterSet {
return CharacterSet(charactersIn: [
CharacterSet(charactersIn: ":/\\"),
.newlines, .controlCharacters, .illegalCharacters
])
}
}