aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/src/uri_string.erl
blob: 50e8a0bf5a6047043792f1f608bbf4546b7453d5 (plain) (blame)
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
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2017. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%
%% [RFC 3986, Chapter 2.2. Reserved Characters]
%%
%%   reserved    = gen-delims / sub-delims
%%
%%   gen-delims  = ":" / "/" / "?" / "#" / "[" / "]" / "@"
%%
%%   sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
%%               / "*" / "+" / "," / ";" / "="
%%
%%
%% [RFC 3986, Chapter 2.3. Unreserved Characters]
%%
%%   unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
%%
%%
%% [RFC 3986, Chapter 3. Syntax Components]
%%
%% The generic URI syntax consists of a hierarchical sequence of
%% components referred to as the scheme, authority, path, query, and
%% fragment.
%%
%%    URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
%%
%%    hier-part   = "//" authority path-abempty
%%                   / path-absolute
%%                   / path-rootless
%%                   / path-empty
%%
%%    The scheme and path components are required, though the path may be
%%    empty (no characters).  When authority is present, the path must
%%    either be empty or begin with a slash ("/") character.  When
%%    authority is not present, the path cannot begin with two slash
%%    characters ("//").  These restrictions result in five different ABNF
%%    rules for a path (Section 3.3), only one of which will match any
%%    given URI reference.
%%
%%    The following are two example URIs and their component parts:
%%
%%          foo://example.com:8042/over/there?name=ferret#nose
%%          \_/   \______________/\_________/ \_________/ \__/
%%           |           |            |            |        |
%%        scheme     authority       path        query   fragment
%%           |   _____________________|__
%%          / \ /                        \
%%          urn:example:animal:ferret:nose
%%
%%
%% [RFC 3986, Chapter 3.1. Scheme]
%%
%% Each URI begins with a scheme name that refers to a specification for
%% assigning identifiers within that scheme.
%%
%%    scheme      = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
%%
%%
%% [RFC 3986, Chapter 3.2. Authority]
%%
%% Many URI schemes include a hierarchical element for a naming
%% authority so that governance of the name space defined by the
%% remainder of the URI is delegated to that authority (which may, in
%% turn, delegate it further).
%%
%%    authority   = [ userinfo "@" ] host [ ":" port ]
%%
%%
%% [RFC 3986, Chapter 3.2.1. User Information]
%%
%% The userinfo subcomponent may consist of a user name and, optionally,
%% scheme-specific information about how to gain authorization to access
%% the resource. The user information, if present, is followed by a
%% commercial at-sign ("@") that delimits it from the host.
%%
%%    userinfo    = *( unreserved / pct-encoded / sub-delims / ":" )
%%
%%
%% [RFC 3986, Chapter 3.2.2. Host]
%%
%% The host subcomponent of authority is identified by an IP literal
%% encapsulated within square brackets, an IPv4 address in dotted-
%% decimal form, or a registered name.
%%
%%    host        = IP-literal / IPv4address / reg-name
%%
%%    IP-literal = "[" ( IPv6address / IPvFuture  ) "]"
%%
%%    IPvFuture  = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
%%
%%    IPv6address =                            6( h16 ":" ) ls32
%%                /                       "::" 5( h16 ":" ) ls32
%%                / [               h16 ] "::" 4( h16 ":" ) ls32
%%                / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
%%                / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
%%                / [ *3( h16 ":" ) h16 ] "::"    h16 ":"   ls32
%%                / [ *4( h16 ":" ) h16 ] "::"              ls32
%%                / [ *5( h16 ":" ) h16 ] "::"              h16
%%                / [ *6( h16 ":" ) h16 ] "::"
%%
%%    ls32        = ( h16 ":" h16 ) / IPv4address
%%                ; least-significant 32 bits of address
%%
%%    h16         = 1*4HEXDIG
%%                ; 16 bits of address represented in hexadecimal
%%
%%    IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
%%
%%    dec-octet   = DIGIT                 ; 0-9
%%                / %x31-39 DIGIT         ; 10-99
%%                / "1" 2DIGIT            ; 100-199
%%                / "2" %x30-34 DIGIT     ; 200-249
%%                / "25" %x30-35          ; 250-255
%%
%%    reg-name    = *( unreserved / pct-encoded / sub-delims )
%%
%%
%% [RFC 3986, Chapter 3.2.2. Port]
%%
%% The port subcomponent of authority is designated by an optional port
%% number in decimal following the host and delimited from it by a
%% single colon (":") character.
%%
%%    port        = *DIGIT
%%
%%
%% [RFC 3986, Chapter 3.3. Path]
%%
%% The path component contains data, usually organized in hierarchical
%% form, that, along with data in the non-hierarchical query component
%% (Section 3.4), serves to identify a resource within the scope of the
%% URI's scheme and naming authority (if any).  The path is terminated
%% by the first question mark ("?") or number sign ("#") character, or
%% by the end of the URI.
%%
%%    path          = path-abempty    ; begins with "/" or is empty
%%                  / path-absolute   ; begins with "/" but not "//"
%%                  / path-noscheme   ; begins with a non-colon segment
%%                  / path-rootless   ; begins with a segment
%%                  / path-empty      ; zero characters
%%
%%    path-abempty  = *( "/" segment )
%%    path-absolute = "/" [ segment-nz *( "/" segment ) ]
%%    path-noscheme = segment-nz-nc *( "/" segment )
%%    path-rootless = segment-nz *( "/" segment )
%%    path-empty    = 0<pchar>
%%    segment       = *pchar
%%    segment-nz    = 1*pchar
%%    segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
%%                  ; non-zero-length segment without any colon ":"
%%
%%    pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
%%
%%
%% [RFC 3986, Chapter 3.4. Query]
%%
%% The query component contains non-hierarchical data that, along with
%% data in the path component (Section 3.3), serves to identify a
%% resource within the scope of the URI's scheme and naming authority
%% (if any).  The query component is indicated by the first question
%% mark ("?") character and terminated by a number sign ("#") character
%% or by the end of the URI.
%%
%%    query       = *( pchar / "/" / "?" )
%%
%%
%% [RFC 3986, Chapter 3.5. Fragment]
%%
%% The fragment identifier component of a URI allows indirect
%% identification of a secondary resource by reference to a primary
%% resource and additional identifying information.
%%
%%    fragment    = *( pchar / "/" / "?" )
%%
%%
%% [RFC 3986, Chapter 4.1. URI Reference]
%%
%% URI-reference is used to denote the most common usage of a resource
%% identifier.
%%
%%    URI-reference = URI / relative-ref
%%
%%
%% [RFC 3986, Chapter 4.2. Relative Reference]
%%
%% A relative reference takes advantage of the hierarchical syntax
%% (Section 1.2.3) to express a URI reference relative to the name space
%% of another hierarchical URI.
%%
%%    relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
%%
%%    relative-part = "//" authority path-abempty
%%                  / path-absolute
%%                  / path-noscheme
%%                  / path-empty
%%
%%
%% [RFC 3986, Chapter 4.3. Absolute URI]
%%
%% Some protocol elements allow only the absolute form of a URI without
%% a fragment identifier.  For example, defining a base URI for later
%% use by relative references calls for an absolute-URI syntax rule that
%% does not allow a fragment.
%%
%%    absolute-URI  = scheme ":" hier-part [ "?" query ]
%%
-module(uri_string).

-export([compose_query/1, create_uri_reference/2, dissect_query/1, normalize/1,
         parse/1, recompose/1, resolve_uri_reference/2, transcode/2]).
-export([is_host/1, is_path/1]).  % suppress warnings
-export_type([uri_map/0, uri_string/0]).

-define(CHAR(Char), <<Char/utf8>>).
-define(STRING_EMPTY, <<>>).
-define(STRING(MatchStr), <<MatchStr/binary>>).
-define(STRING_REST(MatchStr, Rest), <<MatchStr/utf8, Rest/binary>>).


%%%=========================================================================
%%%  API
%%%=========================================================================

%%-------------------------------------------------------------------------
%% URI compliant with RFC 3986
%% ASCII %x21 - %x7A ("!" - "z") except
%%   %x34    "    double quote
%%   %x60    <    less than
%%   %x62    >    greater than
%%   %x92    \    backslash
%%   %x94    ^    caret / circumflex
%%   %x96    `    grave / accent
%%-------------------------------------------------------------------------
-type uri_string() :: iodata().


%% RFC 3986, Chapter 3. Syntax Components
-type uri_map() ::
  #{fragment => unicode:chardata(),
    host => unicode:chardata(),
    path => unicode:chardata(),
    port => non_neg_integer(),
    query => unicode:chardata(),
    scheme => unicode:chardata(),
    userinfo => unicode:chardata()} | #{}.

%% Parse URIs
-spec parse(URIString) -> URIMap when
      URIString :: uri_string(),
      URIMap :: uri_map().
parse(URIString) ->
    if is_binary(URIString) ->
            parse_uri_reference(URIString, #{});
       true ->
            parse_uri_reference(URIString, [], #{})
    end.

%% Recompose URIs
-spec recompose(URIMap) -> URIString when
      URIMap :: uri_map(),
      URIString :: uri_string().
recompose(_) ->
    "".

%% Resolve references
-spec resolve_uri_reference(RelativeURI, AbsoluteBaseURI) -> AbsoluteDestURI when
      RelativeURI :: uri_string(),
      AbsoluteBaseURI :: uri_string(),
      AbsoluteDestURI :: uri_string().
resolve_uri_reference(_,_) ->
    "".

%% Create references
-spec create_uri_reference(AbsoluteSourceURI, AbsoluteBaseURI) -> RelativeDestURI when
      AbsoluteSourceURI :: uri_string(),
      AbsoluteBaseURI :: uri_string(),
      RelativeDestURI :: uri_string().
create_uri_reference(_,_) ->
    "".

%% Normalize URIs
-spec normalize(URIString) -> NormalizedURI when
      URIString :: uri_string(),
      NormalizedURI :: uri_string().
normalize(_) ->
    "".

%% Transcode URIs
-spec transcode(URIString, Options) -> URIString when
      URIString :: uri_string(),
      Options :: [{in_encoding, unicode:encoding()}|{out_encoding, unicode:encoding()}].
transcode(_, _) ->
    "".


%% Working with query strings
%% HTML 2.0 - application/x-www-form-urlencoded
%% RFC 1866 [8.2.1]

%% Compose urlencoded query string from a list of unescaped key/value pairs.
-spec compose_query(QueryList) -> QueryString when
      QueryList :: [{unicode:chardata(), unicode:chardata()}],
      QueryString :: uri_string().
compose_query(_) ->
    "".

%% Dissect a query string into a list of unescaped key/value pairs.
-spec dissect_query(QueryString) -> QueryList when
      QueryString :: uri_string(),
      QueryList :: [{unicode:chardata(), unicode:chardata()}].
dissect_query(_) ->
     "".


%%%========================================================================
%%% Internal functions
%%%========================================================================


%%-------------------------------------------------------------------------
%% [RFC 3986, Chapter 4.1. URI Reference]
%%
%% URI-reference is used to denote the most common usage of a resource
%% identifier.
%%
%%    URI-reference = URI / relative-ref
%%-------------------------------------------------------------------------
-spec parse_uri_reference(iolist(), list(), uri_map()) -> uri_map().
parse_uri_reference([], _, _) -> #{};
parse_uri_reference(URIString, Acc, URI) ->
    try parse_scheme_start(URIString, Acc, URI) of
        Res -> Res
    catch
        throw:uri_parse_error ->
            parse_relative_part(URIString, Acc, URI)
    end.

-spec parse_uri_reference(binary(), uri_map()) -> uri_map().
parse_uri_reference(<<>>, _) -> #{};
parse_uri_reference(URIString, URI) ->
    try parse_scheme_start(URIString, URI) of
        Res -> Res
    catch
        throw:uri_parse_error ->
            parse_relative_part(URIString, URI)
    end.


%%-------------------------------------------------------------------------
%% [RFC 3986, Chapter 4.2. Relative Reference]
%%
%% A relative reference takes advantage of the hierarchical syntax
%% (Section 1.2.3) to express a URI reference relative to the name space
%% of another hierarchical URI.
%%
%%    relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
%%
%%    relative-part = "//" authority path-abempty
%%                  / path-absolute
%%                  / path-noscheme
%%                  / path-empty
%%-------------------------------------------------------------------------
-spec parse_relative_part(binary(), uri_map()) -> uri_map().
parse_relative_part(?STRING_REST("//", Rest), URI) ->
    %% Parse userinfo - "//" is NOT part of authority
    try parse_userinfo(Rest, URI) of
        {T, URI1} ->
	    {Userinfo, _} = split_binary(Rest, byte_size(Rest) - byte_size(T) - 1),
	    URI1#{userinfo => decode_userinfo(Userinfo)}
    catch
        throw:uri_parse_error ->
            {T, URI1} = parse_host(Rest, URI),
	    {Host, _} = split_binary(Rest, byte_size_exl_single_slash(Rest) - byte_size_exl_head(T)),
	    URI1#{host => decode_host(remove_brackets(Host))}
    end;
parse_relative_part(?STRING_REST($/, Rest), URI) ->
    {T, URI1} = parse_segment(Rest, URI),  % path-absolute
    {Path, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    URI1#{path => decode_path(?STRING_REST($/, Path))};
parse_relative_part(?STRING_REST($?, Rest), URI) ->
    {T, URI1} = parse_query(Rest, URI),  % path-empty ?query
    {Query, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    URI1#{query => decode_query(?STRING_REST($?, Query))};
parse_relative_part(?STRING_REST($#, Rest), URI) ->
    {T, URI1} = parse_fragment(Rest, URI),  % path-empty
    {Fragment, _} = split_binary(Rest, byte_size(Rest) - byte_size(T)),
    URI1#{fragment => decode_fragment(Fragment)};
parse_relative_part(?STRING_REST(Char, Rest), URI) ->
    case is_segment_nz_nc(Char) of
        true ->
            {T, URI1} = parse_segment_nz_nc(Rest, URI),  % path-noscheme
            {Path, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
            URI1#{path => decode_path(?STRING_REST(Char, Path))};
        false -> throw(uri_parse_error)
    end.

-spec parse_relative_part(iolist(), list(), uri_map()) -> uri_map().
parse_relative_part([H|Rest], Acc, URI) when is_binary(H) ->
    parse_relative_part(unicode:characters_to_list(H, utf8) ++ Rest,
               Acc, URI);
parse_relative_part([H|Rest], Acc, URI) when is_list(H) ->
    parse_relative_part(H ++ Rest, Acc, URI);
parse_relative_part("//" ++ Rest, Acc, URI) ->
     % Parse userinfo
     try parse_userinfo(Rest, Acc, URI) of
         Res -> Res
     catch
         throw:uri_parse_error ->
             parse_host(Rest, Acc, URI)
     end;
parse_relative_part([$/|Rest], _Acc, URI) ->
    parse_segment(Rest, [$/], URI);  % path-absolute
parse_relative_part([$?|Rest], _Acc, URI) ->
    parse_query(Rest, [$?], URI);  % path-empty ?query
parse_relative_part([$#|Rest], _Acc, URI) ->
    parse_fragment(Rest, [], URI);  % path-empty
parse_relative_part([Char|Rest], _, URI) ->
    case is_segment_nz_nc(Char) of
        true -> parse_segment_nz_nc(Rest, [Char], URI);  % path-noscheme
        false -> throw(uri_parse_error)
    end.


%% Returns size of 'Rest' for proper calculation of splitting position.
%% Solves the following special case:
%%
%%    #{host := <<>>, path := <<"/">>} = uri_string:parse(<<"///">>).
%%
%% While keeping the following true:
%%
%%     #{host := <<"hostname">>} = uri_string:parse(<<"//hostname">>).
%%     #{host := <<>>, path := <<"/hostname">>} = uri_string:parse(<<"///hostname">>).
%%
-spec byte_size_exl_single_slash(uri_string()) -> number().
byte_size_exl_single_slash(<<$/>>) -> 0;
byte_size_exl_single_slash(Rest) -> byte_size(Rest).


%%-------------------------------------------------------------------------
%% [RFC 3986, Chapter 3.3. Path]
%%
%% The path component contains data, usually organized in hierarchical
%% form, that, along with data in the non-hierarchical query component
%% (Section 3.4), serves to identify a resource within the scope of the
%% URI's scheme and naming authority (if any).  The path is terminated
%% by the first question mark ("?") or number sign ("#") character, or
%% by the end of the URI.
%%
%%    path          = path-abempty    ; begins with "/" or is empty
%%                  / path-absolute   ; begins with "/" but not "//"
%%                  / path-noscheme   ; begins with a non-colon segment
%%                  / path-rootless   ; begins with a segment
%%                  / path-empty      ; zero characters
%%
%%    path-abempty  = *( "/" segment )
%%    path-absolute = "/" [ segment-nz *( "/" segment ) ]
%%    path-noscheme = segment-nz-nc *( "/" segment )
%%    path-rootless = segment-nz *( "/" segment )
%%    path-empty    = 0<pchar>
%%    segment       = *pchar
%%    segment-nz    = 1*pchar
%%    segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
%%                  ; non-zero-length segment without any colon ":"
%%
%%    pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
%%-------------------------------------------------------------------------

%%-------------------------------------------------------------------------
%%    path-abempty
%%-------------------------------------------------------------------------
-spec parse_segment(binary(), uri_map()) -> {binary(), uri_map()}.
parse_segment(?STRING_REST($/, Rest), URI) ->
    parse_segment(Rest, URI);  % segment
parse_segment(?STRING_REST($?, Rest), URI) ->
    {T, URI1} = parse_query(Rest, URI),  % ?query
    {Query, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{query => decode_query(?STRING_REST($?, Query))}};
parse_segment(?STRING_REST($#, Rest), URI) ->
    {T, URI1} = parse_fragment(Rest, URI),
    {Fragment, _} = split_binary(Rest, byte_size(Rest) - byte_size(T)),
    {Rest, URI1#{fragment => decode_fragment(Fragment)}};
parse_segment(?STRING_REST(Char, Rest), URI) ->
    case is_pchar(Char) of
        true -> parse_segment(Rest, URI);
        false -> throw(uri_parse_error)
    end;
parse_segment(?STRING_EMPTY, URI) ->
    {?STRING_EMPTY, URI}.

-spec parse_segment(iolist(), list(), uri_map()) -> uri_map().
parse_segment(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_segment(unicode:characters_to_list(Str), Acc, URI);
parse_segment([H|Rest], Acc, URI) when is_binary(H) ->
    parse_segment(unicode:characters_to_list(H, utf8) ++ Rest,
               Acc, URI);
parse_segment([H|Rest], Acc, URI) when is_list(H) ->
    parse_segment(H ++ Rest, Acc, URI);
parse_segment([$/|Rest], Acc, URI) ->
    parse_segment(Rest, [$/|Acc], URI);  % segment
parse_segment([$?|Rest], Acc, URI) ->
    parse_query(Rest, [$?], URI#{path => decode_path(lists:reverse(Acc))});  % ?query
parse_segment([$#|Rest], Acc, URI) ->
    parse_fragment(Rest, [], URI#{path => decode_path(lists:reverse(Acc))});
parse_segment([Char|Rest], Acc, URI) ->
    case is_pchar(Char) of
        true -> parse_segment(Rest, [Char|Acc], URI);
        false -> throw(uri_parse_error)
    end;
parse_segment([], Acc, URI) ->
    URI#{path => decode_path(lists:reverse(Acc))}.

%%-------------------------------------------------------------------------
%%    path-noscheme
%%-------------------------------------------------------------------------
-spec parse_segment_nz_nc(binary(), uri_map()) -> {binary(), uri_map()}.
parse_segment_nz_nc(?STRING_REST($/, Rest), URI) ->
    parse_segment(Rest, URI);  % segment
parse_segment_nz_nc(?STRING_REST($?, Rest), URI) ->
    {T, URI1} = parse_query(Rest, URI),  % ?query
    {Query, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{query => decode_query(?STRING_REST($?, Query))}};
parse_segment_nz_nc(?STRING_REST($#, Rest), URI) ->
    {T, URI1} = parse_fragment(Rest, URI),
    {Fragment, _} = split_binary(Rest, byte_size(Rest) - byte_size(T)),
    {Rest, URI1#{fragment => decode_fragment(Fragment)}};
parse_segment_nz_nc(?STRING_REST(Char, Rest), URI) ->
    case is_segment_nz_nc(Char) of
        true -> parse_segment_nz_nc(Rest, URI);
        false -> throw(uri_parse_error)
    end;
parse_segment_nz_nc(?STRING_EMPTY, URI) ->
    {?STRING_EMPTY, URI}.

-spec parse_segment_nz_nc(iolist(), list(), uri_map()) -> uri_map().
parse_segment_nz_nc(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_segment_nz_nc(unicode:characters_to_list(Str), Acc, URI);
parse_segment_nz_nc([H|Rest], Acc, URI) when is_binary(H) ->
    parse_segment_nz_nc(unicode:characters_to_list(H, utf8) ++ Rest,
               Acc, URI);
parse_segment_nz_nc([H|Rest], Acc, URI) when is_list(H) ->
    parse_segment_nz_nc(H ++ Rest, Acc, URI);
parse_segment_nz_nc([$/|Rest], Acc, URI) ->
    parse_segment(Rest, [$/|Acc], URI);  % segment
parse_segment_nz_nc([$?|Rest], Acc, URI) ->
    parse_query(Rest, [$?], URI#{path => decode_path(lists:reverse(Acc))});  % ?query
parse_segment_nz_nc([$#|Rest], Acc, URI) ->
    parse_fragment(Rest, [], URI#{path => decode_path(lists:reverse(Acc))});
parse_segment_nz_nc([Char|Rest], Acc, URI) ->
    case is_segment_nz_nc(Char) of
        true -> parse_segment_nz_nc(Rest, [Char|Acc], URI);
        false -> throw(uri_parse_error)
    end;
parse_segment_nz_nc([], Acc, URI) ->
    URI#{path => decode_path(lists:reverse(Acc))}.

%% Check if char is pchar.
-spec is_pchar(char()) -> boolean().
is_pchar($%) -> true;  % pct-encoded
is_pchar($:) -> true;
is_pchar($@) -> true;
is_pchar(Char) -> is_unreserved(Char) orelse is_sub_delim(Char).

%% Check if char is segment_nz_nc.
-spec is_segment_nz_nc(char()) -> boolean().
is_segment_nz_nc($%) -> true;  % pct-encoded
is_segment_nz_nc($@) -> true;
is_segment_nz_nc(Char) -> is_unreserved(Char) orelse is_sub_delim(Char).


%%-------------------------------------------------------------------------
%% [RFC 3986, Chapter 3.1. Scheme]
%%
%% Each URI begins with a scheme name that refers to a specification for
%% assigning identifiers within that scheme.
%%
%%    scheme      = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
%%-------------------------------------------------------------------------
-spec parse_scheme_start(binary(), uri_map()) -> uri_map().
parse_scheme_start(?STRING_REST(Char, Rest), URI) ->
    case is_alpha(Char) of
        true  -> {T, URI1} = parse_scheme(Rest, URI),
		 {Scheme, _} = split_binary(Rest, byte_size(Rest) - byte_size(T) - 1),
		 URI1#{scheme => ?STRING_REST(Char, Scheme)};
        false -> throw(uri_parse_error)
    end.

-spec parse_scheme_start(iolist(), list(), uri_map()) -> uri_map().
parse_scheme_start([H|Rest], Acc, URI) when is_binary(H) ->
    parse_scheme_start(unicode:characters_to_list(H, utf8) ++ Rest,
                       Acc, URI);
parse_scheme_start([H|Rest], Acc, URI) when is_list(H) ->
    parse_scheme_start(H ++ Rest, Acc, URI);
parse_scheme_start([Char|Rest], Acc, URI) ->
    case is_alpha(Char) of
        true  -> parse_scheme(Rest, [Char|Acc], URI);
        false -> throw(uri_parse_error)
    end.


-spec parse_scheme(binary(), uri_map()) -> {binary(), uri_map()}.
parse_scheme(?STRING_REST($:, Rest), URI) ->
    {_, URI1} = parse_hier(Rest, URI),
    {Rest, URI1};
parse_scheme(?STRING_REST(Char, Rest), URI) ->
    case is_scheme(Char) of
        true  -> parse_scheme(Rest, URI);
        false -> throw(uri_parse_error)
    end;
parse_scheme(?STRING_EMPTY, _URI) ->
    throw(uri_parse_error).

-spec parse_scheme(iolist(), list(), uri_map()) -> uri_map().
parse_scheme(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_scheme(unicode:characters_to_list(Str), Acc, URI);
parse_scheme([H|Rest], Acc, URI) when is_binary(H) ->
    parse_scheme(unicode:characters_to_list(H, utf8) ++ Rest,
                 Acc, URI);
parse_scheme([H|Rest], Acc, URI) when is_list(H) ->
    parse_scheme(H ++ Rest, Acc, URI);
parse_scheme([$:|Rest], Acc, URI) ->
    parse_hier(Rest, [], URI#{scheme => lists:reverse(Acc)});
parse_scheme([Char|Rest], Acc, URI) ->
    case is_scheme(Char) of
        true  -> parse_scheme(Rest, [Char|Acc], URI);
        false -> throw(uri_parse_error)
    end;
parse_scheme([], _Acc, _URI) ->
    throw(uri_parse_error).

%% Check if char is allowed in scheme
-spec is_scheme(char()) -> boolean().
is_scheme($+) -> true;
is_scheme($-) -> true;
is_scheme($.) -> true;
is_scheme(Char) -> is_alpha(Char) orelse is_digit(Char).


%%-------------------------------------------------------------------------
%%    hier-part   = "//" authority path-abempty
%%                   / path-absolute
%%                   / path-rootless
%%                   / path-empty
%%-------------------------------------------------------------------------
-spec parse_hier(binary(), uri_map()) -> {binary(), uri_map()}.
parse_hier(?STRING_REST("//", Rest), URI) ->
    % Parse userinfo - "//" is NOT part of authority
    try parse_userinfo(Rest, URI) of
        {T, URI1} ->
	    {Userinfo, _} = split_binary(Rest, byte_size(Rest) - byte_size(T) - 1),
	    {Rest, URI1#{userinfo => decode_userinfo(Userinfo)}}
    catch
        throw:uri_parse_error ->
            {T, URI1} = parse_host(Rest, URI),
	    {Host, _} = split_binary(Rest, byte_size_exl_single_slash(Rest) - byte_size_exl_head(T)),
	    {Rest, URI1#{host => decode_host(remove_brackets(Host))}}
    end;
parse_hier(?STRING_REST($/, Rest), URI) ->
    {T, URI1} = parse_segment(Rest, URI),  % path-absolute
    {Path, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{path => decode_path(?STRING_REST($/, Path))}};
parse_hier(?STRING_REST($?, Rest), URI) ->
    {T, URI1} = parse_query(Rest, URI),  % path-empty ?query
    {Query, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{query => decode_query(?STRING_REST($?, Query))}};
parse_hier(?STRING_REST($#, Rest), URI) ->
    {T, URI1} = parse_fragment(Rest, URI),  % path-empty
    {Fragment, _} = split_binary(Rest, byte_size(Rest) - byte_size(T)),
    {Rest, URI1#{fragment => decode_fragment(Fragment)}};
parse_hier(?STRING_REST(Char, Rest), URI) ->  % path-rootless
    case is_pchar(Char) of
        true ->  % segment_nz
            {T, URI1} = parse_segment(Rest, URI),
            {Path, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
            {Rest, URI1#{path => decode_path(?STRING_REST(Char, Path))}};
        false -> throw(uri_parse_error)
    end;
parse_hier(?STRING_EMPTY, URI) ->
    {<<>>, URI}.

-spec parse_hier(iolist(), list(), uri_map()) -> uri_map().
parse_hier(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_hier(unicode:characters_to_list(Str), Acc, URI);
parse_hier([H|Rest], Acc, URI) when is_binary(H) ->
    parse_hier(unicode:characters_to_list(H, utf8) ++ Rest,
               Acc, URI);
parse_hier([H|Rest], Acc, URI) when is_list(H) ->
    parse_hier(H ++ Rest, Acc, URI);
parse_hier("//" ++ Rest, Acc, URI) ->
     % Parse userinfo
     try parse_userinfo(Rest, Acc, URI) of
         Res -> Res
     catch
         throw:uri_parse_error ->
             parse_host(Rest, [], URI)
     end;
parse_hier([$/|Rest], _Acc, URI) ->
    parse_segment(Rest, [$/], URI);  % path-absolute
parse_hier([$?|Rest], _Acc, URI) ->
    parse_query(Rest, [$?], URI);  % path-empty ?query
parse_hier([$#|Rest], _Acc, URI) ->
    parse_fragment(Rest, [], URI);  % path-empty
parse_hier([Char|Rest], _, URI) ->  % path-rootless
    case is_pchar(Char) of
        true -> parse_segment(Rest, [Char], URI);
        false -> throw(uri_parse_error)
    end;
parse_hier([], _, URI) ->
    URI.


%%-------------------------------------------------------------------------
%% [RFC 3986, Chapter 3.2. Authority]
%%
%% Many URI schemes include a hierarchical element for a naming
%% authority so that governance of the name space defined by the
%% remainder of the URI is delegated to that authority (which may, in
%% turn, delegate it further).
%%
%% The authority component is preceded by a double slash ("//") and is
%% terminated by the next slash ("/"), question mark ("?"), or number
%% sign ("#") character, or by the end of the URI.
%%
%%    authority   = [ userinfo "@" ] host [ ":" port ]
%%
%%
%% [RFC 3986, Chapter 3.2.1. User Information]
%%
%% The userinfo subcomponent may consist of a user name and, optionally,
%% scheme-specific information about how to gain authorization to access
%% the resource. The user information, if present, is followed by a
%% commercial at-sign ("@") that delimits it from the host.
%%
%%    userinfo    = *( unreserved / pct-encoded / sub-delims / ":" )
%%-------------------------------------------------------------------------
-spec parse_userinfo(binary(), uri_map()) -> {binary(), uri_map()}.
parse_userinfo(?CHAR($@), _URI) ->
    %% URI cannot end in userinfo state
    throw(uri_parse_error);
parse_userinfo(?STRING_REST($@, Rest), URI) ->
    {T, URI1} = parse_host(Rest, URI),
    {Host, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{host => decode_host(remove_brackets(Host))}};
parse_userinfo(?STRING_REST(Char, Rest), URI) ->
    case is_userinfo(Char) of
        true -> parse_userinfo(Rest, URI);
        false -> throw(uri_parse_error)
    end;
parse_userinfo(?STRING_EMPTY, _URI) ->
    %% URI cannot end in userinfo state
    throw(uri_parse_error).

-spec parse_userinfo(iolist(), list(), uri_map()) -> uri_map().
parse_userinfo(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_userinfo(unicode:characters_to_list(Str), Acc, URI);
parse_userinfo([H|Rest], Acc, URI) when is_binary(H) ->
    parse_userinfo(unicode:characters_to_list(H, utf8) ++ Rest,
                   Acc, URI);
parse_userinfo([H|Rest], Acc, URI) when is_list(H) ->
    parse_userinfo(H ++ Rest, Acc, URI);
parse_userinfo([$@], _Acc, _URI) ->
    %% URI cannot end in userinfo state
    throw(uri_parse_error);
parse_userinfo([$@|Rest], Acc, URI) ->
    parse_host(Rest, [],  URI#{userinfo => decode_userinfo(lists:reverse(Acc))});
parse_userinfo([Char|Rest], Acc, URI) ->
    case is_userinfo(Char) of
        true -> parse_userinfo(Rest, [Char|Acc], URI);
        false -> throw(uri_parse_error)
    end;
parse_userinfo([], _Acc, _URI) ->
    %% URI cannot end in userinfo state
    throw(uri_parse_error).

%% Check if char is allowed in userinfo
-spec is_userinfo(char()) -> boolean().
is_userinfo($%) -> true;  % pct-encoded
is_userinfo($:) -> true;
is_userinfo(Char) -> is_unreserved(Char) orelse is_sub_delim(Char).


%%-------------------------------------------------------------------------
%% [RFC 3986, Chapter 3.2.2. Host]
%%
%% The host subcomponent of authority is identified by an IP literal
%% encapsulated within square brackets, an IPv4 address in dotted-
%% decimal form, or a registered name.
%%
%%    host        = IP-literal / IPv4address / reg-name
%%
%%    IP-literal = "[" ( IPv6address / IPvFuture  ) "]"
%%
%%    IPvFuture  = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
%%
%%    IPv6address =                            6( h16 ":" ) ls32
%%                /                       "::" 5( h16 ":" ) ls32
%%                / [               h16 ] "::" 4( h16 ":" ) ls32
%%                / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
%%                / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
%%                / [ *3( h16 ":" ) h16 ] "::"    h16 ":"   ls32
%%                / [ *4( h16 ":" ) h16 ] "::"              ls32
%%                / [ *5( h16 ":" ) h16 ] "::"              h16
%%                / [ *6( h16 ":" ) h16 ] "::"
%%
%%    ls32        = ( h16 ":" h16 ) / IPv4address
%%                ; least-significant 32 bits of address
%%
%%    h16         = 1*4HEXDIG
%%                ; 16 bits of address represented in hexadecimal
%%
%%    IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
%%
%%    dec-octet   = DIGIT                 ; 0-9
%%                / %x31-39 DIGIT         ; 10-99
%%                / "1" 2DIGIT            ; 100-199
%%                / "2" %x30-34 DIGIT     ; 200-249
%%                / "25" %x30-35          ; 250-255
%%
%%    reg-name    = *( unreserved / pct-encoded / sub-delims )
%%-------------------------------------------------------------------------
-spec parse_host(binary(), uri_map()) -> {binary(), uri_map()}.
parse_host(?STRING_REST($:, Rest), URI) ->
    {T, URI1} = parse_port(Rest, URI),
    {H, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    Port = binary_to_integer(H),
    {Rest, URI1#{port => Port}};
parse_host(?STRING_REST($/, Rest), URI) ->
    {T, URI1} = parse_segment(Rest, URI),  % path-abempty
    {Path, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{path => decode_path(?STRING_REST($/, Path))}};
parse_host(?STRING_REST($?, Rest), URI) ->
    {T, URI1} = parse_query(Rest, URI),  % path-empty ?query
    {Query, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{query => decode_query(?STRING_REST($?, Query))}};
parse_host(?STRING_REST($[, Rest), URI) ->
    parse_ipv6_bin(Rest, [], URI);
parse_host(?STRING_REST($#, Rest), URI) ->
    {T, URI1} = parse_fragment(Rest, URI),  % path-empty
    {Fragment, _} = split_binary(Rest, byte_size(Rest) - byte_size(T)),
    {Rest, URI1#{fragment => decode_fragment(Fragment)}};
parse_host(?STRING_REST(Char, Rest), URI) ->
    case is_digit(Char) of
        true -> parse_ipv4_bin(Rest, [Char], URI);
        false -> parse_reg_name(?STRING_REST(Char, Rest), URI)
    end;
parse_host(?STRING_EMPTY, URI) ->
    {?STRING_EMPTY, URI}.

-spec parse_host(iolist(), list(), uri_map()) -> uri_map().
parse_host(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_host(unicode:characters_to_list(Str), Acc, URI);
parse_host([H|Rest], Acc, URI) when is_binary(H) ->
    parse_host(unicode:characters_to_list(H, utf8) ++ Rest,
               Acc, URI);
parse_host([H|Rest], Acc, URI) when is_list(H) ->
    parse_host(H ++ Rest, Acc, URI);
parse_host([$:|Rest], Acc, URI) ->
    parse_port(Rest, [], URI#{host => decode_host(lists:reverse(Acc))});
parse_host([$/|Rest], Acc, URI) ->
    parse_segment(Rest, [$/], URI#{host => decode_host(lists:reverse(Acc))});  % path-abempty
parse_host([$?|Rest], Acc, URI) ->
    parse_query(Rest, [$?], URI#{host => decode_host(lists:reverse(Acc))});  % path-empty ?query
parse_host([$#|Rest], Acc, URI) ->
    parse_fragment(Rest, [], URI#{host => decode_host(lists:reverse(Acc))});  % path-empty
parse_host([$[|Rest], _Acc, URI) ->
    parse_ipv6(Rest, [], URI);
parse_host([Char|Rest], Acc, URI) ->
    case is_digit(Char) of
        true -> parse_ipv4(Rest, [Char|Acc], URI);
        false -> parse_reg_name([Char|Rest], Acc, URI)
    end;
parse_host([], Acc, URI) ->
    URI#{host => decode_host(lists:reverse(Acc))}.


-spec parse_reg_name(binary(), uri_map()) -> {binary(), uri_map()}.
parse_reg_name(?STRING_REST($:, Rest), URI) ->
    {T, URI1} = parse_port(Rest, URI),
    {H, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    Port = binary_to_integer(H),
    {Rest, URI1#{port => Port}};
parse_reg_name(?STRING_REST($/, Rest), URI) ->
    {T, URI1} = parse_segment(Rest, URI),  % path-abempty
    {Path, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{path => decode_path(?STRING_REST($/, Path))}};
parse_reg_name(?STRING_REST($?, Rest), URI) ->
    {T, URI1} = parse_query(Rest, URI),  % path-empty ?query
    {Query, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{query => decode_query(?STRING_REST($?, Query))}};
parse_reg_name(?STRING_REST($#, Rest), URI) ->
    {T, URI1} = parse_fragment(Rest, URI),  % path-empty
    {Fragment, _} = split_binary(Rest, byte_size(Rest) - byte_size(T)),
    {Rest, URI1#{fragment => decode_fragment(Fragment)}};
parse_reg_name(?STRING_REST(Char, Rest), URI) ->
    case is_reg_name(Char) of
        true -> parse_reg_name(Rest, URI);
        false -> throw(uri_parse_error)
    end;
parse_reg_name(?STRING_EMPTY, URI) ->
    {?STRING_EMPTY, URI}.

-spec parse_reg_name(iolist(), list(), uri_map()) -> uri_map().
parse_reg_name(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_reg_name(unicode:characters_to_list(Str), Acc, URI);
parse_reg_name([H|Rest], Acc, URI) when is_binary(H) ->
    parse_reg_name(unicode:characters_to_list(H, utf8) ++ Rest,
               Acc, URI);
parse_reg_name([H|Rest], Acc, URI) when is_list(H) ->
    parse_reg_name(H ++ Rest, Acc, URI);
parse_reg_name([$:|Rest], Acc, URI) ->
    parse_port(Rest, [], URI#{host => decode_host(lists:reverse(Acc))});
parse_reg_name([$/|Rest], Acc, URI) ->
    parse_segment(Rest, [$/], URI#{host => decode_host(lists:reverse(Acc))});  % path-abempty
parse_reg_name([$?|Rest], Acc, URI) ->
    parse_query(Rest, [$?], URI#{host => decode_host(lists:reverse(Acc))});  % path-empty ?query
parse_reg_name([$#|Rest], Acc, URI) ->
    parse_fragment(Rest, [], URI#{host => decode_host(lists:reverse(Acc))});  % path-empty
parse_reg_name([Char|Rest], Acc, URI) ->
    case is_reg_name(Char) of
        true -> parse_reg_name(Rest, [Char|Acc], URI);
        false -> throw(uri_parse_error)
    end;
parse_reg_name([], Acc, URI) ->
    URI#{host => decode_host(lists:reverse(Acc))}.

%% Check if char is allowed in reg-name
-spec is_reg_name(char()) -> boolean().
is_reg_name($%) -> true;
is_reg_name(Char) -> is_unreserved(Char) orelse is_sub_delim(Char).


-spec parse_ipv4_bin(binary(), list(), uri_map()) -> {binary(), uri_map()}.
parse_ipv4_bin(?STRING_REST($:, Rest), Acc, URI) ->
    _ = validate_ipv4_address(lists:reverse(Acc)),
    {T, URI1} = parse_port(Rest, URI),
    {H, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    Port = binary_to_integer(H),
    {Rest, URI1#{port => Port}};
parse_ipv4_bin(?STRING_REST($/, Rest), Acc, URI) ->
    _ = validate_ipv4_address(lists:reverse(Acc)),
    {T, URI1} = parse_segment(Rest, URI),  % path-abempty
    {Path, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{path => decode_path(?STRING_REST($/, Path))}};
parse_ipv4_bin(?STRING_REST($?, Rest), Acc, URI) ->
    _ = validate_ipv4_address(lists:reverse(Acc)),
    {T, URI1} = parse_query(Rest, URI),  % path-empty ?query
    {Query, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{query => decode_query(?STRING_REST($?, Query))}};
parse_ipv4_bin(?STRING_REST($#, Rest), Acc, URI) ->
    _ = validate_ipv4_address(lists:reverse(Acc)),
    {T, URI1} = parse_fragment(Rest, URI),  % path-empty
    {Fragment, _} = split_binary(Rest, byte_size(Rest) - byte_size(T)),
    {Rest, URI1#{fragment => decode_fragment(Fragment)}};
parse_ipv4_bin(?STRING_REST(Char, Rest), Acc, URI) ->
    case is_ipv4(Char) of
        true -> parse_ipv4_bin(Rest, [Char|Acc], URI);
        false -> throw(uri_parse_error)
    end;
parse_ipv4_bin(?STRING_EMPTY, Acc, URI) ->
    _ = validate_ipv4_address(lists:reverse(Acc)),
    {?STRING_EMPTY, URI}.

-spec parse_ipv4(iolist(), list(), uri_map()) -> uri_map().
parse_ipv4(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_ipv4(unicode:characters_to_list(Str), Acc, URI);
parse_ipv4([H|Rest], Acc, URI) when is_binary(H) ->
    parse_ipv4(unicode:characters_to_list(H, utf8) ++ Rest,
               Acc, URI);
parse_ipv4([H|Rest], Acc, URI) when is_list(H) ->
    parse_ipv4(H ++ Rest, Acc, URI);
parse_ipv4([$:|Rest], Acc, URI) ->
    parse_port(Rest, [], URI#{host => validate_ipv4_address(lists:reverse(Acc))});
parse_ipv4([$/|Rest], Acc, URI) ->
    parse_segment(Rest, [$/], URI#{host => validate_ipv4_address(lists:reverse(Acc))});  % path-abempty
parse_ipv4([$?|Rest], Acc, URI) ->
    parse_query(Rest, [$?], URI#{host => validate_ipv4_address(lists:reverse(Acc))});  % path-empty ?query
parse_ipv4([$#|Rest], Acc, URI) ->
    parse_fragment(Rest, [], URI#{host => validate_ipv4_address(lists:reverse(Acc))});  % path-empty
parse_ipv4([Char|Rest], Acc, URI) ->
    case is_ipv4(Char) of
        true -> parse_ipv4(Rest, [Char|Acc], URI);
        false -> throw(uri_parse_error)
    end;
parse_ipv4([], Acc, URI) ->
    URI#{host => validate_ipv4_address(lists:reverse(Acc))}.

%% Check if char is allowed in IPv4 addresses
-spec is_ipv4(char()) -> boolean().
is_ipv4($.) -> true;
is_ipv4(Char) -> is_digit(Char).

-spec validate_ipv4_address(list()) -> list().
validate_ipv4_address(Addr) ->
    case inet:parse_ipv4strict_address(Addr) of
        {ok, _} -> Addr;
        {error, _} -> throw(uri_parse_error)
    end.


-spec parse_ipv6_bin(binary(), list(), uri_map()) -> {binary(), uri_map()}.
parse_ipv6_bin(?STRING_REST($], Rest), Acc, URI) ->
    _ = validate_ipv6_address(lists:reverse(Acc)),
    parse_ipv6_bin_end(Rest, URI);
parse_ipv6_bin(?STRING_REST(Char, Rest), Acc, URI) ->
    case is_ipv6(Char) of
        true -> parse_ipv6_bin(Rest, [Char|Acc], URI);
        false -> throw(uri_parse_error)
    end;
parse_ipv6_bin(?STRING_EMPTY, _Acc, _URI) ->
    throw(uri_parse_error).

-spec parse_ipv6(iolist(), list(), uri_map()) -> uri_map().
parse_ipv6(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_ipv6(unicode:characters_to_list(Str), Acc, URI);
parse_ipv6([H|Rest], Acc, URI) when is_binary(H) ->
    parse_ipv6(unicode:characters_to_list(H, utf8) ++ Rest,
               Acc, URI);
parse_ipv6([H|Rest], Acc, URI) when is_list(H) ->
    parse_ipv6(H ++ Rest, Acc, URI);
parse_ipv6([$]|Rest], Acc, URI) ->
    parse_ipv6_end(Rest, [], URI#{host => validate_ipv6_address(lists:reverse(Acc))});
parse_ipv6([Char|Rest], Acc, URI) ->
    case is_ipv6(Char) of
        true -> parse_ipv6(Rest, [Char|Acc], URI);
        false ->
            io:format("# DEBUG Char: >>~c<<~n", [Char]),
            io:format("# DEBUG Rest: >>~s<<~n", [Rest]),
            throw(uri_parse_error)
    end;
parse_ipv6([], _Acc, _URI) ->
    throw(uri_parse_error).

%% Check if char is allowed in IPv6 addresses
-spec is_ipv6(char()) -> boolean().
is_ipv6($:) -> true;
is_ipv6($.) -> true;
is_ipv6(Char) -> is_hex_digit(Char).


-spec parse_ipv6_bin_end(binary(), uri_map()) -> {binary(), uri_map()}.
parse_ipv6_bin_end(?STRING_REST($:, Rest), URI) ->
    {T, URI1} = parse_port(Rest, URI),
    {H, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    Port = binary_to_integer(H),
    {Rest, URI1#{port => Port}};
parse_ipv6_bin_end(?STRING_REST($/, Rest), URI) ->
    {T, URI1} = parse_segment(Rest, URI),  % path-abempty
    {Path, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{path => decode_path(?STRING_REST($/, Path))}};
parse_ipv6_bin_end(?STRING_REST($?, Rest), URI) ->
    {T, URI1} = parse_query(Rest, URI),  % path-empty ?query
    {Query, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{query => decode_query(?STRING_REST($?, Query))}};
parse_ipv6_bin_end(?STRING_REST($#, Rest), URI) ->
    {T, URI1} = parse_fragment(Rest, URI),  % path-empty
    {Fragment, _} = split_binary(Rest, byte_size(Rest) - byte_size(T)),
    {Rest, URI1#{fragment => decode_fragment(Fragment)}};
parse_ipv6_bin_end(?STRING_REST(Char, Rest), URI) ->
    case is_ipv6(Char) of
        true -> parse_ipv6_bin_end(Rest, URI);
        false -> throw(uri_parse_error)
    end;
parse_ipv6_bin_end(?STRING_EMPTY, URI) ->
    {?STRING_EMPTY, URI}.

-spec parse_ipv6_end(iolist(), list(), uri_map()) -> uri_map().
parse_ipv6_end(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_ipv6_end(unicode:characters_to_list(Str), Acc, URI);
parse_ipv6_end([H|Rest], Acc, URI) when is_binary(H) ->
    parse_ipv6_end(unicode:characters_to_list(H, utf8) ++ Rest,
               Acc, URI);
parse_ipv6_end([H|Rest], Acc, URI) when is_list(H) ->
    parse_ipv6_end(H ++ Rest, Acc, URI);
parse_ipv6_end([$:|Rest], _Acc, URI) ->
    parse_port(Rest, [], URI);
parse_ipv6_end([$/|Rest], _Acc, URI) ->
    parse_segment(Rest, [$/], URI);  % path-abempty
parse_ipv6_end([$?|Rest], _Acc, URI) ->
    parse_query(Rest, [$?], URI);  % path-empty ?query
parse_ipv6_end([$#|Rest], _Acc, URI) ->
    parse_fragment(Rest, [], URI);  % path-empty
parse_ipv6_end([], _Acc, URI) ->
    URI.


-spec validate_ipv6_address(list()) -> list().
validate_ipv6_address(Addr) ->
    case inet:parse_ipv6strict_address(Addr) of
        {ok, _} -> Addr;
        {error, _} -> throw(uri_parse_error)
    end.


%%-------------------------------------------------------------------------
%% [RFC 3986, Chapter 3.2.2. Port]
%%
%% The port subcomponent of authority is designated by an optional port
%% number in decimal following the host and delimited from it by a
%% single colon (":") character.
%%
%%    port        = *DIGIT
%%-------------------------------------------------------------------------
-spec parse_port(binary(), uri_map()) -> {binary(), uri_map()}.
parse_port(?STRING_REST($/, Rest), URI) ->
    {T, URI1} = parse_segment(Rest, URI),  % path-abempty
    {Path, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{path => decode_path(?STRING_REST($/, Path))}};
parse_port(?STRING_REST($?, Rest), URI) ->
    {T, URI1} = parse_query(Rest, URI),  % path-empty ?query
    {Query, _} = split_binary(Rest, byte_size(Rest) - byte_size_exl_head(T)),
    {Rest, URI1#{query => decode_query(?STRING_REST($?, Query))}};
parse_port(?STRING_REST($#, Rest), URI) ->
    {T, URI1} = parse_fragment(Rest, URI),  % path-empty
    {Fragment, _} = split_binary(Rest, byte_size(Rest) - byte_size(T)),
    {Rest, URI1#{fragment => decode_fragment(Fragment)}};
parse_port(?STRING_REST(Char, Rest), URI) ->
    case is_digit(Char) of
        true -> parse_port(Rest, URI);
        false -> throw(uri_parse_error)
    end;
parse_port(?STRING_EMPTY, URI) ->
    {?STRING_EMPTY, URI}.

-spec parse_port(iolist(), list(), uri_map()) -> uri_map().
parse_port(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_port(unicode:characters_to_list(Str), Acc, URI);
parse_port([H|Rest], Acc, URI) when is_binary(H) ->
    parse_port(unicode:characters_to_list(H, utf8) ++ Rest,
               Acc, URI);
parse_port([H|Rest], Acc, URI) when is_list(H) ->
    parse_port(H ++ Rest, Acc, URI);
parse_port([$/|Rest], Acc, URI) ->
    {Port, _} = string:to_integer(lists:reverse(Acc)),
    parse_segment(Rest, [$/], URI#{port => Port});  % path-abempty
parse_port([$?|Rest], Acc, URI) ->
    {Port, _} = string:to_integer(lists:reverse(Acc)),
    parse_query(Rest, [$?], URI#{port => Port});  % path-empty ?query
parse_port([$#|Rest], Acc, URI) ->
    {Port, _} = string:to_integer(lists:reverse(Acc)),
    parse_fragment(Rest, [], URI#{port => Port});  % path-empty
parse_port([Char|Rest], Acc, URI) ->
    case is_digit(Char) of
        true -> parse_port(Rest, [Char|Acc], URI);
        false -> throw(uri_parse_error)
    end;
parse_port([], Acc, URI) ->
    {Port, _} = string:to_integer(lists:reverse(Acc)),
    URI#{port => Port}.


%%-------------------------------------------------------------------------
%% [RFC 3986, Chapter 3.4. Query]
%%
%% The query component contains non-hierarchical data that, along with
%% data in the path component (Section 3.3), serves to identify a
%% resource within the scope of the URI's scheme and naming authority
%% (if any).  The query component is indicated by the first question
%% mark ("?") character and terminated by a number sign ("#") character
%% or by the end of the URI.
%%
%%    query       = *( pchar / "/" / "?" )
%%-------------------------------------------------------------------------
-spec parse_query(binary(), uri_map()) -> {binary(), uri_map()}.
parse_query(?STRING_REST($#, Rest), URI) ->
    {T, URI1} = parse_fragment(Rest, URI),
    {Fragment, _} = split_binary(Rest, byte_size(Rest) - byte_size(T)),
    {Rest, URI1#{fragment => decode_fragment(Fragment)}};
parse_query(?STRING_REST(Char, Rest), URI) ->
    case is_query(Char) of
        true -> parse_query(Rest, URI);
        false -> throw(uri_parse_error)
    end;
parse_query(?STRING_EMPTY, URI) ->
    {?STRING_EMPTY, URI}.

-spec parse_query(iolist(), list(), uri_map()) -> uri_map().
parse_query(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_query(unicode:characters_to_list(Str), Acc, URI);
parse_query([H|Rest], Acc, URI) when is_binary(H) ->
    parse_query(unicode:characters_to_list(H, utf8) ++ Rest,
               Acc, URI);
parse_query([H|Rest], Acc, URI) when is_list(H) ->
    parse_query(H ++ Rest, Acc, URI);
parse_query([$#|Rest], Acc, URI) ->
    parse_fragment(Rest, [], URI#{query => decode_query(lists:reverse(Acc))});
parse_query([Char|Rest], Acc, URI) ->
    case is_query(Char) of
        true -> parse_query(Rest, [Char|Acc], URI);
        false -> throw(uri_parse_error)
    end;
parse_query([], Acc, URI) ->
    URI#{query => decode_query(lists:reverse(Acc))}.

%% Check if char is allowed in query
-spec is_query(char()) -> boolean().
is_query($/) -> true;
is_query($?) -> true;
is_query(Char) -> is_pchar(Char).


%%-------------------------------------------------------------------------
%% [RFC 3986, Chapter 3.5. Fragment]
%%
%% The fragment identifier component of a URI allows indirect
%% identification of a secondary resource by reference to a primary
%% resource and additional identifying information.
%%
%%    fragment    = *( pchar / "/" / "?" )
%%-------------------------------------------------------------------------
-spec parse_fragment(binary(), uri_map()) -> {binary(), uri_map()}.
parse_fragment(?STRING_REST(Char, Rest), URI) ->
    case is_fragment(Char) of
        true -> parse_fragment(Rest, URI);
        false -> throw(uri_parse_error)
    end;
parse_fragment(?STRING_EMPTY, URI) ->
    {?STRING_EMPTY, URI}.

-spec parse_fragment(iolist(), list(), uri_map()) -> uri_map().
parse_fragment(?STRING(Str), Acc, URI) when is_list(Acc) ->
    parse_fragment(unicode:characters_to_list(Str), Acc, URI);
parse_fragment([H|Rest], Acc, URI) when is_binary(H) ->
    parse_fragment(unicode:characters_to_list(H, utf8) ++ Rest,
                   Acc, URI);
parse_fragment([H|Rest], Acc, URI) when is_list(H) ->
    parse_fragment(H ++ Rest, Acc, URI);
parse_fragment([Char|Rest], Acc, URI) ->
    case is_fragment(Char) of
        true -> parse_fragment(Rest, [Char|Acc], URI);
        false -> throw(uri_parse_error)
    end;
parse_fragment([], Acc, URI) ->
    URI#{fragment => decode_fragment(lists:reverse(Acc))}.

%% Check if char is allowed in fragment
-spec is_fragment(char()) -> boolean().
is_fragment($/) -> true;
is_fragment($?) -> true;
is_fragment(Char) -> is_pchar(Char).


%%-------------------------------------------------------------------------
%% [RFC 3986, Chapter 2.2. Reserved Characters]
%%
%%   reserved    = gen-delims / sub-delims
%%
%%   gen-delims  = ":" / "/" / "?" / "#" / "[" / "]" / "@"
%%
%%   sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
%%               / "*" / "+" / "," / ";" / "="
%%
%%-------------------------------------------------------------------------
%% %% Return true if input char is reserved.
%% -spec is_reserved(char()) -> boolean().
%% is_reserved(Char) ->
%%     is_gen_delim(Char) orelse is_sub_delim(Char).

%% %% Check if char is reserved.
%% -spec is_gen_delim(char()) -> boolean().
%% is_gen_delim($:) -> true;
%% is_gen_delim($/) -> true;
%% is_gen_delim($?) -> true;
%% is_gen_delim($#) -> true;
%% is_gen_delim($[) -> true;
%% is_gen_delim($]) -> true;
%% is_gen_delim($@) -> true;
%% is_gen_delim(_) -> false.

%% Check if char is sub-delim.
-spec is_sub_delim(char()) -> boolean().
is_sub_delim($!) -> true;
is_sub_delim($$) -> true;
is_sub_delim($&) -> true;
is_sub_delim($') -> true;
is_sub_delim($() -> true;
is_sub_delim($)) -> true;

is_sub_delim($*) -> true;
is_sub_delim($+) -> true;
is_sub_delim($,) -> true;
is_sub_delim($;) -> true;
is_sub_delim($=) -> true;
is_sub_delim(_) -> false.


%%-------------------------------------------------------------------------
%% [RFC 3986, Chapter 2.3. Unreserved Characters]
%%
%%   unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
%%
%%-------------------------------------------------------------------------
-spec is_unreserved(char()) -> boolean().
is_unreserved($-) -> true;
is_unreserved($.) -> true;
is_unreserved($_) -> true;
is_unreserved($~) -> true;
is_unreserved(Char) -> is_alpha(Char) orelse is_digit(Char).

-spec is_alpha(char()) -> boolean().
is_alpha(C)
  when $A =< C, C =< $Z;
       $a =< C, C =< $z -> true;
is_alpha(_) -> false.

-spec is_digit(char()) -> boolean().
is_digit(C)
  when $0 =< C, C =< $9 -> true;
is_digit(_) -> false.

-spec is_hex_digit(char()) -> boolean().
is_hex_digit(C)
  when $0 =< C, C =< $9;$a =< C, C =< $f;$A =< C, C =< $F -> true;
is_hex_digit(_) -> false.

%% Returns the size of a binary exluding the first element.
%% Used in calls to split_binary().
-spec byte_size_exl_head(binary()) -> number().
byte_size_exl_head(<<>>) -> 0;
byte_size_exl_head(Binary) -> byte_size(Binary) + 1.

% Remove brackets from binary
-spec remove_brackets(binary()) -> binary().
remove_brackets(?STRING_REST($[,Addr)) ->
    A1 = binary:replace(Addr, <<$[>>, <<>>),
    binary:replace(A1, <<$]>>, <<>>);
remove_brackets(Addr) -> Addr.


%%-------------------------------------------------------------------------
%% [RFC 3986, Chapter 2.1.  Percent-Encoding]
%%
%% A percent-encoding mechanism is used to represent a data octet in a
%% component when that octet's corresponding character is outside the
%% allowed set or is being used as a delimiter of, or within, the
%% component.  A percent-encoded octet is encoded as a character
%% triplet, consisting of the percent character "%" followed by the two
%% hexadecimal digits representing that octet's numeric value.  For
%% example, "%20" is the percent-encoding for the binary octet
%% "00100000" (ABNF: %x20), which in US-ASCII corresponds to the space
%% character (SP).  Section 2.4 describes when percent-encoding and
%% decoding is applied.
%%
%%   pct-encoded = "%" HEXDIG HEXDIG
%%-------------------------------------------------------------------------
-spec decode_userinfo(list()|binary()) -> list() | binary().
decode_userinfo(Cs) ->
    decode(Cs, fun is_userinfo/1, <<>>).


-spec decode_host(list()|binary()) -> list() | binary().
decode_host(Cs) ->
    decode(Cs, fun is_host/1, <<>>).

%% Check if char is allowed in host
-spec is_host(char()) -> boolean().
is_host($:) -> true;
is_host(Char) -> is_unreserved(Char) orelse is_sub_delim(Char).


-spec decode_path(list()|binary()) -> list() | binary().
decode_path(Cs) ->
    decode(Cs, fun is_path/1, <<>>).

%% Check if char is allowed in path
-spec is_path(char()) -> boolean().
is_path($/) -> true;

is_path(Char) -> is_pchar(Char).


-spec decode_query(list()|binary()) -> list() | binary().
decode_query(Cs) ->
    decode(Cs, fun is_query/1, <<>>).

-spec decode_fragment(list()|binary()) -> list() | binary().
decode_fragment(Cs) ->
    decode(Cs, fun is_host/1, <<>>).


-spec decode(list()|binary(), fun(), binary()) -> list() | binary().
decode(<<$%,C0,C1,Cs/binary>>, Fun, Acc) ->
    case is_hex_digit(C0) andalso is_hex_digit(C1) of
        true ->
            B = hex2dec(C0)*16+hex2dec(C1),
            decode(Cs, Fun, <<Acc/binary, B>>);
        false -> throw(uri_parse_error)
    end;
decode(<<C,Cs/binary>>, Fun, Acc) ->
    case Fun(C) of
        true -> decode(Cs, Fun, <<Acc/binary, C>>);
        false -> throw(uri_parse_error)
    end;
decode(<<>>, _Fun, Acc) ->
    Acc;
decode([$%,C0,C1|Cs], Fun, Acc) ->
    case is_hex_digit(C0) andalso is_hex_digit(C1) of
        true ->
            B = hex2dec(C0)*16+hex2dec(C1),
            decode(Cs, Fun, <<Acc/binary, B>>);
        false -> throw(uri_parse_error)
    end;
decode([C|Cs], Fun, Acc) ->
    case Fun(C) of
        true -> decode(Cs, Fun, <<Acc/binary, C>>);
        false -> throw(uri_parse_error)
    end;
decode([], _Fun, Acc) ->
    unicode:characters_to_list(Acc).


hex2dec(X) when (X >= $0) andalso (X =< $9) -> X - $0;
hex2dec(X) when (X >= $A) andalso (X =< $F) -> X - $A + 10;
hex2dec(X) when (X >= $a) andalso (X =< $f) -> X - $a + 10.