File: URLComponents.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (1306 lines) | stat: -rw-r--r-- 57,796 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#if FOUNDATION_FRAMEWORK
internal import _ForSwiftFoundation
#endif

/// A structure designed to parse URLs based on RFC 3986 and to construct URLs from their constituent parts.
///
/// You can easily obtain a `URL` based on the contents of a `URLComponents` or vice versa.
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
public struct URLComponents: Hashable, Equatable, Sendable {
    var components: _URLComponents

    internal enum InvalidComponentError: Error {
        case scheme
        case user
        case password
        case host
        case port
        case path
        case query
        case queryItem
        case fragment
    }

    internal enum Component {
        case scheme
        case user
        case password
        case host
        case port
        case path
        case query
        case queryItem
        case fragment
    }

    internal struct _URLComponents: Sendable {

        typealias Parser = RFC3986Parser

        /// Non-nil if the components were initialized from a string, which is parsed into a `URLParseInfo`.
        private var urlParseInfo: URLParseInfo?
        private var parseInfoIsValidForAllRanges:   Bool = true
        private var parseInfoIsValidForScheme:      Bool = true
        private var parseInfoIsValidForUser:        Bool = true
        private var parseInfoIsValidForPassword:    Bool = true
        private var parseInfoIsValidForHost:        Bool = true
        private var parseInfoIsValidForPort:        Bool = true
        private var parseInfoIsValidForPath:        Bool = true
        private var parseInfoIsValidForQuery:       Bool = true
        private var parseInfoIsValidForFragment:    Bool = true

        /// Non-nil if that component was set directly (e.g. `components.scheme = "http"`).
        /// If non-nil, the string will always be valid and properly encoded.  The public computed vars
        /// (non-underscored) first check these underlying stores if present, then check the `urlParseInfo`.
        private var _scheme: String?
        private var _user: String?
        private var _password: String?
        private var _host: String?
        private var _port: Int?
        private var _path: String?
        private var _query: String?
        private var _fragment: String?

        /// True if host was percent-encoded instead of IDNA-encoded.
        private var didPercentEncodeHost: Bool = false

        /// If IDNA-encoding fails to create a valid host string, set this
        /// to `true` to force `.string` and `.url` to return `nil`.
        private var didSetInvalidHost: Bool = false

        init() {
            parseInfoIsValidForAllRanges    = false
            parseInfoIsValidForScheme       = false
            parseInfoIsValidForUser         = false
            parseInfoIsValidForPassword     = false
            parseInfoIsValidForHost         = false
            parseInfoIsValidForPort         = false
            parseInfoIsValidForPath         = false
            parseInfoIsValidForQuery        = false
            parseInfoIsValidForFragment     = false
        }

        init?(string: String, encodingInvalidCharacters: Bool = true) {
            guard let parseInfo = Parser.parse(urlString: string, encodingInvalidCharacters: encodingInvalidCharacters) else {
                return nil
            }
            urlParseInfo = parseInfo
            didPercentEncodeHost = parseInfo.didPercentEncodeHost
        }

        init(parseInfo: URLParseInfo) {
            urlParseInfo = parseInfo
            didPercentEncodeHost = parseInfo.didPercentEncodeHost
        }

        /// Resets the given component. When setting any component, we must:
        /// 1) Forget the cached string if present.
        /// 2) Mark the parse info as invalid, meaning its `urlString` can no longer be used for ranges.
        /// 3) Cause the parse info to forget that this component exists by setting its respective range to nil.
        /// Note that other components in the parse info are still valid if their respective range is non-nil.
        private mutating func reset(_ component: Component) {
            parseInfoIsValidForAllRanges = false
            switch component {
            case .scheme:
                parseInfoIsValidForScheme = false
            case .user:
                parseInfoIsValidForUser = false
            case .password:
                parseInfoIsValidForPassword = false
            case .host:
                parseInfoIsValidForHost = false
                didPercentEncodeHost = false
                didSetInvalidHost = false
            case .port:
                parseInfoIsValidForPort = false
            case .path:
                parseInfoIsValidForPath = false
            case .query:
                parseInfoIsValidForQuery = false
            case .queryItem:
                parseInfoIsValidForQuery = false
            case .fragment:
                parseInfoIsValidForFragment = false
            }
        }

        var scheme: String? {
            if let _scheme { return _scheme }
            if parseInfoIsValidForScheme, let scheme = urlParseInfo?.scheme { return String(scheme) }
            return nil
        }

        mutating func setScheme(_ newValue: String?) throws {
            reset(.scheme)
            guard Parser.validate(newValue, component: .scheme) else {
                throw InvalidComponentError.scheme
            }
            _scheme = newValue
            if encodedHost != nil {
                // This resets the host to an appropriate encoding for the given scheme
                let decodedHost = host
                host = decodedHost
            }
        }

        var user: String? {
            get { Parser.percentDecode(percentEncodedUser) }
            set {
                reset(.user)
                if Parser.validate(newValue, component: .user, percentEncodingAllowed: false) {
                    _user = newValue
                    return
                }
                _user = Parser.percentEncode(newValue, component: .user)
            }
        }

        var password: String? {
            get { Parser.percentDecode(percentEncodedPassword) }
            set {
                reset(.password)
                if Parser.validate(newValue, component: .password, percentEncodingAllowed: false) {
                    _password = newValue
                    return
                }
                _password = Parser.percentEncode(newValue, component: .password)
            }
        }

        var host: String? {
            get {
                guard let encodedHost else { return nil }
                guard !encodedHost.isEmpty else { return "" }
                if didPercentEncodeHost {
                    return Parser.percentDecode(encodedHost)
                } else {
                    return Parser.IDNADecodeHost(encodedHost)
                }
            }
            set {
                reset(.host)
                guard let newValue else {
                    _host = nil
                    return
                }
                if Parser.validate(newValue, component: .host) {
                    _host = newValue
                    didPercentEncodeHost = newValue.utf8.contains(UInt8(ascii: "%"))
                    return
                }
                if Parser.shouldPercentEncodeHost(newValue, forScheme: scheme) {
                    guard let percentEncoded = Parser.percentEncode(newValue, component: .host) else {
                        _host = nil
                        didSetInvalidHost = true
                        return
                    }
                    _host = percentEncoded
                    didPercentEncodeHost = true
                    return
                }
                if let idnaEncoded = Parser.IDNAEncodeHost(newValue),
                   Parser.validate(idnaEncoded, component: .host) {
                    _host = idnaEncoded
                    return
                }
                // Even if the IDNA-encoded host is not valid, we need to
                // keep it around to see if a special-cased scheme is set
                // later, telling us to percent-encode it instead.
                // Keep the valid percent-encoded version for now.
                didSetInvalidHost = true
                _host = Parser.percentEncode(newValue, component: .host)
                didPercentEncodeHost = true
            }
        }

        var port: Int? {
            _port ?? (parseInfoIsValidForPort ? urlParseInfo?.port : nil)
        }

        mutating func setPort(_ newValue: Int?) throws {
            reset(.port)
            if let newValue, newValue < 0 {
                throw InvalidComponentError.port
            }
            _port = newValue
        }

        var path: String {
            get { Parser.percentDecode(percentEncodedPath) ?? "" }
            set {
                reset(.path)
                _path = Parser.percentEncode(newValue, component: .path) ?? ""
            }
        }

        var query: String? {
            get { Parser.percentDecode(percentEncodedQuery) }
            set {
                reset(.query)
                if Parser.validate(newValue, component: .query, percentEncodingAllowed: false) {
                    _query = newValue
                    return
                }
                _query = Parser.percentEncode(newValue, component: .query)
            }
        }

        var fragment: String? {
            get { Parser.percentDecode(percentEncodedFragment) }
            set {
                reset(.fragment)
                if Parser.validate(newValue, component: .fragment, percentEncodingAllowed: false) {
                    _fragment = newValue
                    return
                }
                _fragment = Parser.percentEncode(newValue, component: .fragment)
            }
        }

        var percentEncodedUser: String? {
            if let _user { return _user }
            if parseInfoIsValidForUser, let user = urlParseInfo?.user { return String(user) }
            if percentEncodedPassword != nil { return "" }
            return nil
        }

        mutating func setPercentEncodedUser(_ newValue: String?) throws {
            reset(.user)
            guard Parser.validate(newValue, component: .user) else {
                throw InvalidComponentError.user
            }
            _user = newValue
        }

        var percentEncodedPassword: String? {
            if let _password { return _password }
            if parseInfoIsValidForPassword, let password = urlParseInfo?.password { return String(password) }
            return nil
        }

        mutating func setPercentEncodedPassword(_ newValue: String?) throws {
            reset(.password)
            guard Parser.validate(newValue, component: .password) else {
                throw InvalidComponentError.password
            }
            _password = newValue
        }

        var percentEncodedHost: String? {
            if let encodedHost {
                if encodedHost.isEmpty { return "" }
                if didPercentEncodeHost { return encodedHost }
                // Undo any IDNA-encoding and return the percent-encoded version.
                if let decoded = Parser.IDNADecodeHost(encodedHost),
                   let percentEncoded = Parser.percentEncode(decoded, component: .host) {
                    return percentEncoded
                }
            }
            if port != nil || percentEncodedUser != nil { return "" }
            return nil
        }

        mutating func setPercentEncodedHost(_ newValue: String?) throws {
            reset(.host)
            guard let newValue else {
                _host = nil
                return
            }
            guard Parser.validate(newValue, component: .host) else {
                throw InvalidComponentError.host
            }
            didPercentEncodeHost = newValue.utf8.contains(UInt8(ascii: "%"))
            if Parser.shouldPercentEncodeHost(newValue, forScheme: scheme) {
                _host = newValue
                return
            }
            if didPercentEncodeHost {
                guard let decoded = Parser.percentDecode(newValue) else {
                    _host = newValue
                    didSetInvalidHost = true
                    return
                }
                host = decoded
                return
            }
            _host = newValue
        }

        var encodedHost: String? {
            if let _host { return _host }
            if parseInfoIsValidForHost, let host = urlParseInfo?.host { return String(host) }
            if port != nil || percentEncodedUser != nil { return "" }
            return nil
        }

        mutating func setEncodedHost(_ newValue: String?) throws {
            reset(.host)
            guard let newValue else {
                _host = nil
                return
            }
            guard Parser.validate(newValue, component: .host) else {
                throw InvalidComponentError.host
            }
            _host = newValue
            didPercentEncodeHost = newValue.utf8.contains(UInt8(ascii: "%"))
        }

        var percentEncodedPath: String {
            if let _path { return _path }
            if parseInfoIsValidForPath, let path = urlParseInfo?.path { return String(path) }
            return ""
        }

        mutating func setPercentEncodedPath(_ newValue: String) throws {
            reset(.path)
            guard Parser.validate(newValue, component: .path) else {
                throw InvalidComponentError.path
            }
            _path = newValue
        }

        var percentEncodedQuery: String? {
            if let _query { return _query }
            if parseInfoIsValidForQuery, let query = urlParseInfo?.query { return String(query) }
            return nil
        }

        mutating func setPercentEncodedQuery(_ newValue: String?) throws {
            reset(.query)
            guard Parser.validate(newValue, component: .query) else {
                throw InvalidComponentError.query
            }
            _query = newValue
        }

        var percentEncodedFragment: String? {
            if let _fragment { return _fragment }
            if parseInfoIsValidForFragment, let fragment = urlParseInfo?.fragment { return String(fragment) }
            return nil
        }

        mutating func setPercentEncodedFragment(_ newValue: String?) throws {
            reset(.fragment)
            guard Parser.validate(newValue, component: .fragment) else {
                throw InvalidComponentError.fragment
            }
            _fragment = newValue
        }

        var string: String? {
            if parseInfoIsValidForAllRanges { return urlParseInfo?.urlString }
            return computedString
        }

        private var hasAuthority: Bool {
            encodedHost != nil || port != nil || percentEncodedUser != nil || percentEncodedPassword != nil
        }

        private var computedString: String? {
            if didSetInvalidHost { return nil }
            var result = ""
            if let scheme {
                result += "\(scheme):"
            }
            if hasAuthority {
                if let first = percentEncodedPath.utf8.first, first != UInt8(ascii: "/") {
                    return nil
                }
                result += "//"
            } else {
                // If there is no authority, do not allow the path to start with "//",
                // which could be mistaken for the authority separator.
                let pathUTF8 = percentEncodedPath.utf8
                let pathStartsWithDoubleSlash = (
                    pathUTF8.index(after: pathUTF8.startIndex) != pathUTF8.endIndex &&
                    UInt8(ascii: "/") == pathUTF8.first &&
                    UInt8(ascii: "/") == pathUTF8[pathUTF8.index(after: pathUTF8.startIndex)]
                )
                guard !pathStartsWithDoubleSlash else {
                    return nil
                }
            }
            if let percentEncodedUser {
                result += percentEncodedUser
            }
            if let percentEncodedPassword {
                result += ":\(percentEncodedPassword)"
            }
            if percentEncodedUser != nil || percentEncodedPassword != nil {
                result += "@"
            }
            if let encodedHost {
                result += encodedHost
            }
            if let port {
                result += ":\(port)"
            } else if parseInfoIsValidForPort, let portString = urlParseInfo?.portString {
                // The parser already validated a special-case (e.g. addressbook:).
                result += ":\(portString)"
            }
            result += percentEncodedPath
            if let percentEncodedQuery {
                result += "?\(percentEncodedQuery)"
            }
            if let percentEncodedFragment {
                result += "#\(percentEncodedFragment)"
            }
            return result
        }

        func rangeOf(_ component: Component) -> Range<String.Index>? {
            if let urlParseInfo, parseInfoIsValidForAllRanges {
                switch component {
                case .scheme:
                    return urlParseInfo.schemeRange
                case .user:
                    return urlParseInfo.userRange
                case .password:
                    return urlParseInfo.passwordRange
                case .host:
                    return urlParseInfo.hostRange
                case .port:
                    return urlParseInfo.portRange
                case .path:
                    return urlParseInfo.pathRange
                case .query:
                    return urlParseInfo.queryRange
                case .queryItem:
                    return nil
                case .fragment:
                    return urlParseInfo.fragmentRange
                }
            }
            guard let string, let parseInfo = Parser.parse(urlString: string, encodingInvalidCharacters: true) else {
                return nil
            }
            switch component {
            case .scheme:
                return parseInfo.schemeRange
            case .user:
                return parseInfo.userRange
            case .password:
                return parseInfo.passwordRange
            case .host:
                return parseInfo.hostRange
            case .port:
                return parseInfo.portRange
            case .path:
                return parseInfo.pathRange
            case .query:
                return parseInfo.queryRange
            case .queryItem:
                return nil
            case .fragment:
                return parseInfo.fragmentRange
            }
        }

        func queryItems(percentEncoded: Bool) -> [URLQueryItem]? {
            guard let percentEncodedQuery else { return nil }
            guard !percentEncodedQuery.isEmpty else { return [] }
            var result: [URLQueryItem] = []

            let queryUTF8 = percentEncodedQuery.utf8
            var currentIndex = queryUTF8.startIndex
            var itemStartIndex = queryUTF8.startIndex
            var equalSignIndex: String.Index?

            func addItem() {
                // Called when currentIndex is at query item end boundary (either "&" or endIndex).
                var name = ""
                var value: String?
                if let equalSignIndex {
                    name = String(percentEncodedQuery[itemStartIndex..<equalSignIndex])
                    let valueStartIndex = queryUTF8.index(after: equalSignIndex)
                    value = String(percentEncodedQuery[valueStartIndex..<currentIndex])
                } else {
                    name = String(percentEncodedQuery[itemStartIndex..<currentIndex])
                }
                if !percentEncoded {
                    name = Parser.percentDecode(name) ?? ""
                    value = Parser.percentDecode(value)
                }
                result.append(URLQueryItem(name: name, value: value))
            }

            while currentIndex != queryUTF8.endIndex {
                switch queryUTF8[currentIndex] {
                case UInt8(ascii: "="):
                    if equalSignIndex == nil {
                        equalSignIndex = currentIndex
                    }
                case UInt8(ascii: "&"):
                    addItem()
                    itemStartIndex = queryUTF8.index(after: currentIndex)
                    equalSignIndex = nil
                default:
                    break
                }
                currentIndex = queryUTF8.index(after: currentIndex)
            }
            // Add the final query item.
            addItem()
            return result
        }

        mutating func setQueryItems(_ newValue: [URLQueryItem]?) {
            reset(.query)
            guard let newValue else {
                _query = nil
                return
            }
            guard !newValue.isEmpty else {
                _query = ""
                return
            }

            _query = newValue.map { item in
                var itemStr = ""
                if Parser.validate(item.name, component: .queryItem, percentEncodingAllowed: false) {
                    itemStr += item.name
                } else if let percentEncodedName = Parser.percentEncode(item.name, component: .queryItem) {
                    itemStr += percentEncodedName
                }
                guard let value = item.value else {
                    return itemStr
                }
                itemStr += "="
                if Parser.validate(value, component: .queryItem, percentEncodingAllowed: false) {
                    itemStr += value
                } else if let percentEncodedValue = Parser.percentEncode(value, component: .queryItem) {
                    itemStr += percentEncodedValue
                }
                return itemStr
            }.joined(separator: "&")
        }

        mutating func setPercentEncodedQueryItems(_ newValue: [URLQueryItem]?) throws {
            reset(.query)
            guard let newValue else {
                _query = nil
                return
            }
            guard !newValue.isEmpty else {
                _query = ""
                return
            }

            _query = try newValue.map { item in
                guard Parser.validate(item.name, component: .queryItem) else {
                    throw InvalidComponentError.queryItem
                }
                var itemStr = item.name
                if let value = item.value {
                    guard Parser.validate(value, component: .query) else {
                        throw InvalidComponentError.queryItem
                    }
                    itemStr += "=\(value)"
                }
                return itemStr
            }.joined(separator: "&")
        }
    }

    /// Initialize with all components undefined.
    public init() {
        self.components = _URLComponents()
    }

    /// Initialize with the components of a URL.
    ///
    /// If resolvingAgainstBaseURL is `true` and url is a relative URL, the components of url.absoluteURL are used. If the url string from the URL is malformed, nil is returned.
    public init?(url: __shared URL, resolvingAgainstBaseURL resolve: Bool) {
        let string: String
        if resolve {
            string = url.absoluteString
        } else {
            string = url.relativeString
        }
        guard let components = _URLComponents(string: string) else {
            return nil
        }
        self.components = components
    }

    /// Initialize with a URL string.
    ///
    /// If the URLString is malformed, nil is returned.
    public init?(string: __shared String) {
        guard let components = _URLComponents(string: string) else {
            return nil
        }
        self.components = components
    }

    /// Initialize with a URL string and the option to add (or skip) IDNA- and percent-encoding of invalid characters.
    /// If `encodingInvalidCharacters` is false, and the URL string is invalid according to RFC 3986, `nil` is returned.
    /// If `encodingInvalidCharacters` is true, `URLComponents` will try to encode the string to create a valid URL.
    /// If the URL string is still invalid after encoding, `nil` is returned.
    ///
    /// - Parameter string: The URL string.
    /// - Parameter encodingInvalidCharacters: True if `URLComponents` should try to encode an invalid URL string, false otherwise.
    /// - Returns: A `URLComponents` struct for a valid URL, or `nil` if the URL is invalid.
    @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
    public init?(string: __shared String, encodingInvalidCharacters: Bool) {
        guard let components = _URLComponents(string: string, encodingInvalidCharacters: encodingInvalidCharacters) else {
            return nil
        }
        self.components = components
    }

    internal init(parseInfo: URLParseInfo) {
        self.components = _URLComponents(parseInfo: parseInfo)
    }

    /// Returns a URL created from the URLComponents.
    ///
    /// If the URLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string. If the NSURLComponents does not have an authority component (user, password, host or port) and has a path component, the path component must not start with "//". If those requirements are not met, nil is returned.
    public var url: URL? {
        guard let string else { return nil }
        #if FOUNDATION_FRAMEWORK
        guard foundation_swift_url_enabled() else {
            return CFURLCreateWithString(kCFAllocatorDefault, string as CFString, nil) as URL?
        }
        #endif
        return URL(string: string)
    }

    /// Returns a URL created from the URLComponents relative to a base URL.
    ///
    /// If the URLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string. If the URLComponents does not have an authority component (user, password, host or port) and has a path component, the path component must not start with "//". If those requirements are not met, nil is returned.
    public func url(relativeTo base: URL?) -> URL? {
        guard let string else { return nil }
        guard let base else { return url }
        #if FOUNDATION_FRAMEWORK
        guard foundation_swift_url_enabled() else {
            return CFURLCreateWithString(kCFAllocatorDefault, string as CFString, base as CFURL) as URL?
        }
        #endif
        return URL(string: string, relativeTo: base)
    }

    /// Returns a URL string created from the URLComponents.
    ///
    /// If the URLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string. If the URLComponents does not have an authority component (user, password, host or port) and has a path component, the path component must not start with "//". If those requirements are not met, nil is returned.
    @available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
    public var string: String? {
        components.string
    }

    /// The scheme subcomponent of the URL.
    ///
    /// The getter for this property removes any percent encoding this component may have (if the component allows percent encoding). Setting this property assumes the subcomponent or component string is not percent encoded and will add percent encoding (if the component allows percent encoding).
    /// Attempting to set the scheme with an invalid scheme string will cause an exception.
    public var scheme: String? {
        get { components.scheme }
        set {
            do {
                try components.setScheme(newValue)
            } catch {
                fatalError("Attempting to set scheme with invalid characters")
            }
        }
    }

#if FOUNDATION_FRAMEWORK
    /// Throwing function used by `_NSSwiftURLComponents` to generate an exception for ObjC callers
    internal mutating func setScheme(_ newValue: String?) throws {
        try components.setScheme(newValue)
    }
#endif

    /// The user subcomponent of the URL.
    ///
    /// The getter for this property removes any percent encoding this component may have (if the component allows percent encoding). Setting this property assumes the subcomponent or component string is not percent encoded and will add percent encoding (if the component allows percent encoding).
    ///
    /// Warning: IETF STD 66 (rfc3986) says the use of the format "user:password" in the userinfo subcomponent of a URI is deprecated because passing authentication information in clear text has proven to be a security risk. However, there are cases where this practice is still needed, and so the user and password components and methods are provided.
    public var user: String? {
        get { components.user }
        set { components.user = newValue }
    }

    /// The password subcomponent of the URL.
    ///
    /// The getter for this property removes any percent encoding this component may have (if the component allows percent encoding). Setting this property assumes the subcomponent or component string is not percent encoded and will add percent encoding (if the component allows percent encoding).
    ///
    /// Warning: IETF STD 66 (rfc3986) says the use of the format "user:password" in the userinfo subcomponent of a URI is deprecated because passing authentication information in clear text has proven to be a security risk. However, there are cases where this practice is still needed, and so the user and password components and methods are provided.
    public var password: String? {
        get { components.password }
        set { components.password = newValue }
    }

    /// The host subcomponent.
    ///
    /// The getter for this property removes any percent encoding this component may have (if the component allows percent encoding). Setting this property assumes the subcomponent or component string is not percent encoded and will add percent encoding (if the component allows percent encoding).
    public var host: String? {
        get { components.host }
        set { components.host = newValue }
    }

    /// The port subcomponent.
    ///
    /// The getter for this property removes any percent encoding this component may have (if the component allows percent encoding). Setting this property assumes the subcomponent or component string is not percent encoded and will add percent encoding (if the component allows percent encoding).
    /// Attempting to set a negative port number will cause a fatal error.
    public var port: Int? {
        get { components.port }
        set {
            do {
                try components.setPort(newValue)
            } catch {
                fatalError("Attempting to set port with a negative number")
            }
        }
    }

#if FOUNDATION_FRAMEWORK
    /// Throwing function used by `_NSSwiftURLComponents` to generate an exception for ObjC callers
    internal mutating func setPort(_ newValue: Int?) throws {
        try components.setPort(newValue)
    }
#endif

    /// The path subcomponent.
    ///
    /// The getter for this property removes any percent encoding this component may have (if the component allows percent encoding). Setting this property assumes the subcomponent or component string is not percent encoded and will add percent encoding (if the component allows percent encoding).
    public var path: String {
        get { components.path }
        set { components.path = newValue }
    }

    /// The query subcomponent.
    ///
    /// The getter for this property removes any percent encoding this component may have (if the component allows percent encoding). Setting this property assumes the subcomponent or component string is not percent encoded and will add percent encoding (if the component allows percent encoding).
    public var query: String? {
        get { components.query }
        set { components.query = newValue }
    }

    /// The fragment subcomponent.
    ///
    /// The getter for this property removes any percent encoding this component may have (if the component allows percent encoding). Setting this property assumes the subcomponent or component string is not percent encoded and will add percent encoding (if the component allows percent encoding).
    public var fragment: String? {
        get { components.fragment }
        set { components.fragment = newValue }
    }


    /// The user subcomponent, percent-encoded.
    ///
    /// The getter for this property retains any percent encoding this component may have. Setting this properties assumes the component string is already correctly percent encoded. Attempting to set an incorrectly percent encoded string will cause a `fatalError`. Although ';' is a legal path character, it is recommended that it be percent-encoded for best compatibility with `URL` (`String.addingPercentEncoding(withAllowedCharacters:)` will percent-encode any ';' characters if you pass `CharacterSet.urlUserAllowed`).
    public var percentEncodedUser: String? {
        get { components.percentEncodedUser }
        set {
            do {
                try components.setPercentEncodedUser(newValue)
            } catch {
                fatalError("Attempting to set percentEncodedUser with invalid characters")
            }
        }
    }

#if FOUNDATION_FRAMEWORK
    /// Throwing function used by `_NSSwiftURLComponents` to generate an exception for ObjC callers
    internal mutating func setPercentEncodedUser(_ newValue: String?) throws {
        try components.setPercentEncodedUser(newValue)
    }
#endif

    /// The password subcomponent, percent-encoded.
    ///
    /// The getter for this property retains any percent encoding this component may have. Setting this properties assumes the component string is already correctly percent encoded. Attempting to set an incorrectly percent encoded string will cause a `fatalError`. Although ';' is a legal path character, it is recommended that it be percent-encoded for best compatibility with `URL` (`String.addingPercentEncoding(withAllowedCharacters:)` will percent-encode any ';' characters if you pass `CharacterSet.urlPasswordAllowed`).
    public var percentEncodedPassword: String? {
        get { components.percentEncodedPassword }
        set {
            do {
                try components.setPercentEncodedPassword(newValue)
            } catch {
                fatalError("Attempting to set percentEncodedPassword with invalid characters")
            }
        }
    }

#if FOUNDATION_FRAMEWORK
    /// Throwing function used by `_NSSwiftURLComponents` to generate an exception for ObjC callers
    internal mutating func setPercentEncodedPassword(_ newValue: String?) throws {
        try components.setPercentEncodedPassword(newValue)
    }
#endif

    /// The host subcomponent, percent-encoded.
    ///
    /// The getter for this property retains any percent encoding this component may have. Setting this properties assumes the component string is already correctly percent encoded. Attempting to set an incorrectly percent encoded string will cause a `fatalError`. Although ';' is a legal path character, it is recommended that it be percent-encoded for best compatibility with `URL` (`String.addingPercentEncoding(withAllowedCharacters:)` will percent-encode any ';' characters if you pass `CharacterSet.urlHostAllowed`).
    @available(macOS, introduced: 10.10, deprecated: 100000.0, message: "Use encodedHost instead")
    @available(iOS, introduced: 8.0, deprecated: 100000.0, message: "Use encodedHost instead")
    @available(tvOS, introduced: 9.0, deprecated: 100000.0, message: "Use encodedHost instead")
    @available(watchOS, introduced: 2.0, deprecated: 100000.0, message: "Use encodedHost instead")
    @available(visionOS, introduced: 1.0, deprecated: 100000.0, message: "Use encodedHost instead")
    public var percentEncodedHost: String? {
        get { components.percentEncodedHost }
        set {
            do {
                try components.setPercentEncodedHost(newValue)
            } catch {
                fatalError("Attempting to set percentEncodedHost with invalid characters")
            }
        }
    }

#if FOUNDATION_FRAMEWORK
    /// Throwing function used by `_NSSwiftURLComponents` to generate an exception for ObjC callers
    internal mutating func setPercentEncodedHost(_ newValue: String?) throws {
        try components.setPercentEncodedHost(newValue)
    }
#endif

    @available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
    public var encodedHost: String? {
        get { components.encodedHost }
        set {
            do {
                try components.setEncodedHost(newValue)
            } catch {
                fatalError("Attempting to set encodedHost with invalid characters")
            }
        }
    }

#if FOUNDATION_FRAMEWORK
    /// Throwing function used by `_NSSwiftURLComponents` to generate an exception for ObjC callers
    internal mutating func setEncodedHost(_ newValue: String?) throws {
        try components.setEncodedHost(newValue)
    }
#endif

    /// The path subcomponent, percent-encoded.
    ///
    /// The getter for this property retains any percent encoding this component may have. Setting this properties assumes the component string is already correctly percent encoded. Attempting to set an incorrectly percent encoded string will cause a `fatalError`. Although ';' is a legal path character, it is recommended that it be percent-encoded for best compatibility with `URL` (`String.addingPercentEncoding(withAllowedCharacters:)` will percent-encode any ';' characters if you pass `CharacterSet.urlPathAllowed`).
    public var percentEncodedPath: String {
        get { components.percentEncodedPath }
        set {
            do {
                try components.setPercentEncodedPath(newValue)
            } catch {
                fatalError("Attempting to set percentEncodedPath with invalid characters")
            }
        }
    }

#if FOUNDATION_FRAMEWORK
    /// Throwing function used by `_NSSwiftURLComponents` to generate an exception for ObjC callers
    internal mutating func setPercentEncodedPath(_ newValue: String) throws {
        try components.setPercentEncodedPath(newValue)
    }
#endif

    /// The query subcomponent, percent-encoded.
    ///
    /// The getter for this property retains any percent encoding this component may have. Setting this properties assumes the component string is already correctly percent encoded. Attempting to set an incorrectly percent encoded string will cause a `fatalError`. Although ';' is a legal path character, it is recommended that it be percent-encoded for best compatibility with `URL` (`String.addingPercentEncoding(withAllowedCharacters:)` will percent-encode any ';' characters if you pass `CharacterSet.urlQueryAllowed`).
    public var percentEncodedQuery: String? {
        get { components.percentEncodedQuery }
        set {
            do {
                try components.setPercentEncodedQuery(newValue)
            } catch {
                fatalError("Attempting to set percentEncodedQuery with invalid characters")
            }
        }
    }

#if FOUNDATION_FRAMEWORK
    /// Throwing function used by `_NSSwiftURLComponents` to generate an exception for ObjC callers
    internal mutating func setPercentEncodedQuery(_ newValue: String?) throws {
        try components.setPercentEncodedQuery(newValue)
    }
#endif

    /// The fragment subcomponent, percent-encoded.
    ///
    /// The getter for this property retains any percent encoding this component may have. Setting this properties assumes the component string is already correctly percent encoded. Attempting to set an incorrectly percent encoded string will cause a `fatalError`. Although ';' is a legal path character, it is recommended that it be percent-encoded for best compatibility with `URL` (`String.addingPercentEncoding(withAllowedCharacters:)` will percent-encode any ';' characters if you pass `CharacterSet.urlFragmentAllowed`).
    public var percentEncodedFragment: String? {
        get { components.percentEncodedFragment }
        set {
            do {
                try components.setPercentEncodedFragment(newValue)
            } catch {
                fatalError("Attempting to set percentEncodedFragment with invalid characters")
            }
        }
    }

#if FOUNDATION_FRAMEWORK
    /// Throwing function used by `_NSSwiftURLComponents` to generate an exception for ObjC callers
    internal mutating func setPercentEncodedFragment(_ newValue: String?) throws {
        try components.setPercentEncodedFragment(newValue)
    }
#endif

    /// Returns the character range of the scheme in the string returned by `var string`.
    ///
    /// If the component does not exist, nil is returned.
    /// - note: Zero length components are legal. For example, the URL string "scheme://:@/?#" has a zero length user, password, host, query and fragment; the URL strings "scheme:" and "" both have a zero length path.
    @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *)
    public var rangeOfScheme: Range<String.Index>? {
        components.rangeOf(.scheme)
    }

    /// Returns the character range of the user in the string returned by `var string`.
    ///
    /// If the component does not exist, nil is returned.
    /// - note: Zero length components are legal. For example, the URL string "scheme://:@/?#" has a zero length user, password, host, query and fragment; the URL strings "scheme:" and "" both have a zero length path.
    @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *)
    public var rangeOfUser: Range<String.Index>? {
        components.rangeOf(.user)
    }

    /// Returns the character range of the password in the string returned by `var string`.
    ///
    /// If the component does not exist, nil is returned.
    /// - note: Zero length components are legal. For example, the URL string "scheme://:@/?#" has a zero length user, password, host, query and fragment; the URL strings "scheme:" and "" both have a zero length path.
    @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *)
    public var rangeOfPassword: Range<String.Index>? {
        components.rangeOf(.password)
    }

    /// Returns the character range of the host in the string returned by `var string`.
    ///
    /// If the component does not exist, nil is returned.
    /// - note: Zero length components are legal. For example, the URL string "scheme://:@/?#" has a zero length user, password, host, query and fragment; the URL strings "scheme:" and "" both have a zero length path.
    @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *)
    public var rangeOfHost: Range<String.Index>? {
        components.rangeOf(.host)
    }

    /// Returns the character range of the port in the string returned by `var string`.
    ///
    /// If the component does not exist, nil is returned.
    /// - note: Zero length components are legal. For example, the URL string "scheme://:@/?#" has a zero length user, password, host, query and fragment; the URL strings "scheme:" and "" both have a zero length path.
    @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *)
    public var rangeOfPort: Range<String.Index>? {
        components.rangeOf(.port)
    }

    /// Returns the character range of the path in the string returned by `var string`.
    ///
    /// If the component does not exist, nil is returned.
    /// - note: Zero length components are legal. For example, the URL string "scheme://:@/?#" has a zero length user, password, host, query and fragment; the URL strings "scheme:" and "" both have a zero length path.
    @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *)
    public var rangeOfPath: Range<String.Index>? {
        components.rangeOf(.path)
    }

    /// Returns the character range of the query in the string returned by `var string`.
    ///
    /// If the component does not exist, nil is returned.
    /// - note: Zero length components are legal. For example, the URL string "scheme://:@/?#" has a zero length user, password, host, query and fragment; the URL strings "scheme:" and "" both have a zero length path.
    @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *)
    public var rangeOfQuery: Range<String.Index>? {
        components.rangeOf(.query)
    }

    /// Returns the character range of the fragment in the string returned by `var string`.
    ///
    /// If the component does not exist, nil is returned.
    /// - note: Zero length components are legal. For example, the URL string "scheme://:@/?#" has a zero length user, password, host, query and fragment; the URL strings "scheme:" and "" both have a zero length path.
    @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *)
    public var rangeOfFragment: Range<String.Index>? {
        components.rangeOf(.fragment)
    }

    /// Returns an array of query items for this `URLComponents`, in the order in which they appear in the original query string.
    ///
    /// Each `URLQueryItem` represents a single key-value pair,
    ///
    /// Note that a name may appear more than once in a single query string, so the name values are not guaranteed to be unique. If the `URLComponents` has an empty query component, returns an empty array. If the `URLComponents` has no query component, returns nil.
    ///
    /// The setter combines an array containing any number of `URLQueryItem`s, each of which represents a single key-value pair, into a query string and sets the `URLComponents` query property. Passing an empty array sets the query component of the `URLComponents` to an empty string. Passing nil removes the query component of the `URLComponents`.
    ///
    /// - note: If a name-value pair in a query is empty (i.e. the query string starts with '&', ends with '&', or has "&&" within it), you get a `URLQueryItem` with a zero-length name and a nil value. If a query's name-value pair has nothing before the equals sign, you get a zero-length name. If a query's name-value pair has nothing after the equals sign, you get a zero-length value. If a query's name-value pair has no equals sign, the query name-value pair string is the name and you get a nil value.
    @available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
    public var queryItems: [URLQueryItem]? {
        get { components.queryItems(percentEncoded: false) }
        set { components.setQueryItems(newValue) }
    }

    /// Returns an array of query items for this `URLComponents`, in the order in which they appear in the original query string. Any percent-encoding in a query item name or value is retained
    ///
    /// The setter combines an array containing any number of `URLQueryItem`s, each of which represents a single key-value pair, into a query string and sets the `URLComponents` query property. This property assumes the query item names and values are already correctly percent-encoded, and that the query item names do not contain the query item delimiter characters '&' and '='. Attempting to set an incorrectly percent-encoded query item or a query item name with the query item delimiter characters '&' and '=' will cause a `fatalError`.
    @available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
    public var percentEncodedQueryItems: [URLQueryItem]? {
        get { components.queryItems(percentEncoded: true) }
        set {
            do {
                try components.setPercentEncodedQueryItems(newValue)
            } catch {
                fatalError("Attempting to set percentEncodedQueryItems with invalid characters")
            }
        }
    }

#if FOUNDATION_FRAMEWORK
    /// Throwing function used by `_NSSwiftURLComponents` to generate an exception for ObjC callers
    internal mutating func setPercentEncodedQueryItems(_ newValue: [URLQueryItem]?) throws {
        try components.setPercentEncodedQueryItems(newValue)
    }
#endif

    public func hash(into hasher: inout Hasher) {
        hasher.combine(scheme)
        hasher.combine(percentEncodedUser)
        hasher.combine(percentEncodedPassword)
        hasher.combine(encodedHost)
        hasher.combine(port)
        hasher.combine(percentEncodedPath)
        hasher.combine(percentEncodedQuery)
        hasher.combine(percentEncodedFragment)
    }

    // MARK: - Bridging

    public static func ==(lhs: URLComponents, rhs: URLComponents) -> Bool {
        return (
            // Check in (estimated) order of most likely to exist, so we fail faster if non-equal.
            lhs.percentEncodedPath == rhs.percentEncodedPath &&
            lhs.scheme == rhs.scheme &&
            lhs.encodedHost == rhs.encodedHost &&
            lhs.port == rhs.port &&
            lhs.percentEncodedQuery == rhs.percentEncodedQuery &&
            lhs.percentEncodedFragment == rhs.percentEncodedFragment &&
            lhs.percentEncodedUser == rhs.percentEncodedUser &&
            lhs.percentEncodedPassword == rhs.percentEncodedPassword
        )
    }
}

@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
extension URLComponents: CustomStringConvertible, CustomDebugStringConvertible, CustomReflectable {

    public var description: String {
        if let u = url {
            return u.description
        } else {
            return self.customMirror.children.reduce(into: "") {
                $0 += "\($1.label ?? ""): \($1.value) "
            }
        }
    }

    public var debugDescription: String {
        return self.description
    }

    public var customMirror: Mirror {
        var c: [(label: String?, value: Any)] = []

        if let s = self.scheme { c.append((label: "scheme", value: s)) }
        if let u = self.user { c.append((label: "user", value: u)) }
        if let pw = self.password { c.append((label: "password", value: pw)) }
        if let h = self.host { c.append((label: "host", value: h)) }
        if let p = self.port { c.append((label: "port", value: p)) }

        c.append((label: "path", value: self.path))
        if let qi = self.queryItems { c.append((label: "queryItems", value: qi)) }
        if let f = self.fragment { c.append((label: "fragment", value: f)) }
        let m = Mirror(self, children: c, displayStyle: Mirror.DisplayStyle.struct)
        return m
    }
}

#if FOUNDATION_FRAMEWORK
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
extension URLComponents: ReferenceConvertible, _ObjectiveCBridgeable {

    public typealias ReferenceType = NSURLComponents

    public static func _getObjectiveCType() -> Any.Type {
        return NSURLComponents.self
    }

    @_semantics("convertToObjectiveC")
    public func _bridgeToObjectiveC() -> NSURLComponents {
        _NSSwiftURLComponents(components: self)
    }

    public static func _forceBridgeFromObjectiveC(_ x: NSURLComponents, result: inout URLComponents?) {
        if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
            fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
        }
    }

    public static func _conditionallyBridgeFromObjectiveC(_ x: NSURLComponents, result: inout URLComponents?) -> Bool {
        var comp = URLComponents()
        comp.scheme = x.scheme
        comp.user = x.user
        comp.password = x.password
        comp.host = x.host
        comp.port = x.port?.intValue
        comp.path = x.path ?? ""
        comp.query = x.query
        comp.fragment = x.fragment
        result = comp
        return true
    }

    @_effects(readonly)
    public static func _unconditionallyBridgeFromObjectiveC(_ source: NSURLComponents?) -> URLComponents {
        guard let src = source else { return URLComponents() }
        var result: URLComponents? = URLComponents()
        _ = _conditionallyBridgeFromObjectiveC(src, result: &result)
        return result!
    }
}

@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
extension NSURLComponents: _HasCustomAnyHashableRepresentation {
    // Must be @nonobjc to avoid infinite recursion during bridging.
    @nonobjc
    public func _toCustomAnyHashable() -> AnyHashable? {
        return AnyHashable(self as URLComponents)
    }
}
#endif // FOUNDATION_FRAMEWORK


/// A single name-value pair, for use with `URLComponents`.
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
public struct URLQueryItem: Hashable, Equatable, Sendable {

    public var name: String
    public var value: String?

    public init(name: __shared String, value: __shared String?) {
        self.name = name
        self.value = value
    }

    public func hash(into hasher: inout Hasher) {
        hasher.combine(name)
        hasher.combine(value)
    }

    @available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
    public static func ==(lhs: URLQueryItem, rhs: URLQueryItem) -> Bool {
        return lhs.name == rhs.name && lhs.value == rhs.value
    }
}

@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
extension URLQueryItem: CustomStringConvertible, CustomDebugStringConvertible, CustomReflectable {

    public var description: String {
        if let v = value {
            return "\(name)=\(v)"
        } else {
            return name
        }
    }

    public var debugDescription: String {
        return self.description
    }

    public var customMirror: Mirror {
        let c: [(label: String?, value: Any)] = [
            ("name", name),
            ("value", value as Any),
        ]
        return Mirror(self, children: c, displayStyle: Mirror.DisplayStyle.struct)
    }
}

#if FOUNDATION_FRAMEWORK
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
extension URLQueryItem: ReferenceConvertible, _ObjectiveCBridgeable {

    public typealias ReferenceType = NSURLQueryItem

    public static func _getObjectiveCType() -> Any.Type {
        return NSURLQueryItem.self
    }

    @_semantics("convertToObjectiveC")
    public func _bridgeToObjectiveC() -> NSURLQueryItem {
        return _NSSwiftURLQueryItem(queryItem: self)
    }

    public static func _forceBridgeFromObjectiveC(_ x: NSURLQueryItem, result: inout URLQueryItem?) {
        if !_conditionallyBridgeFromObjectiveC(x, result: &result) {
            fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)")
        }
    }

    public static func _conditionallyBridgeFromObjectiveC(_ x: NSURLQueryItem, result: inout URLQueryItem?) -> Bool {
        result = URLQueryItem(name: x.name, value: x.value)
        return true
    }

    @_effects(readonly)
    public static func _unconditionallyBridgeFromObjectiveC(_ source: NSURLQueryItem?) -> URLQueryItem {
        var result: URLQueryItem?
        _forceBridgeFromObjectiveC(source!, result: &result)
        return result!
    }
}

@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
extension NSURLQueryItem: _HasCustomAnyHashableRepresentation {
    // Must be @nonobjc to avoid infinite recursion during bridging.
    @nonobjc
    public func _toCustomAnyHashable() -> AnyHashable? {
        return AnyHashable(self as URLQueryItem)
    }
}
#endif // FOUNDATION_FRAMEWORK

@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
extension URLComponents: Codable {
    private enum CodingKeys: Int, CodingKey {
        case scheme
        case user
        case password
        case host
        case port
        case path
        case query
        case fragment
    }

    public init(from decoder: Decoder) throws {
        self.init()

        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.scheme = try container.decodeIfPresent(String.self, forKey: .scheme)
        self.user = try container.decodeIfPresent(String.self, forKey: .user)
        self.password = try container.decodeIfPresent(String.self, forKey: .password)
        self.host = try container.decodeIfPresent(String.self, forKey: .host)
        self.port = try container.decodeIfPresent(Int.self, forKey: .port)
        self.path = try container.decode(String.self, forKey: .path)
        self.query = try container.decodeIfPresent(String.self, forKey: .query)
        self.fragment = try container.decodeIfPresent(String.self, forKey: .fragment)
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(self.scheme, forKey: .scheme)
        try container.encodeIfPresent(self.user, forKey: .user)
        try container.encodeIfPresent(self.password, forKey: .password)
        try container.encodeIfPresent(self.host, forKey: .host)
        try container.encodeIfPresent(self.port, forKey: .port)
        try container.encode(self.path, forKey: .path)
        try container.encodeIfPresent(self.query, forKey: .query)
        try container.encodeIfPresent(self.fragment, forKey: .fragment)
    }
}