File: validate.go

package info (click to toggle)
golang-github-seancfoley-ipaddress-go 1.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 3,700 kB
  • sloc: makefile: 3
file content (4397 lines) | stat: -rw-r--r-- 173,192 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
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
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
//
// Copyright 2020-2022 Sean C Foley
//
// 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.
//

package ipaddr

import (
	"math"
	"math/big"
	"strings"
	"sync"
	"sync/atomic"
	"unicode"
	"unsafe"

	"github.com/seancfoley/ipaddress-go/ipaddr/addrerr"
	"github.com/seancfoley/ipaddress-go/ipaddr/addrstrparam"
)

var chars, extendedChars = createChars()

func createChars() (chars [int('z') + 1]byte, extendedChars [int('~') + 1]byte) {
	i := byte(1)
	for c := '1'; i < 10; i, c = i+1, c+1 {
		chars[c] = i
	}
	for c, c2 := 'a', 'A'; i < 26; i, c, c2 = i+1, c+1, c2+1 {
		chars[c] = i
		chars[c2] = i
	}
	var extendedDigits = []byte{
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
		'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
		'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
		'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
		'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
		'y', 'z', '!', '#', '$', '%', '&', '(', ')', '*', '+', '-',
		';', '<', '=', '>', '?', '@', '^', '_', '`', '{', '|', '}',
		'~'}
	extLen := byte(len(extendedDigits))
	for i = 0; i < extLen; i++ {
		c := extendedDigits[i]
		extendedChars[c] = i
	}
	return
}

const (
	longSize                           = 64
	maxHostLength                      = 253
	maxHostSegments                    = 127
	maxLabelLength                     = 63
	macDoubleSegmentDigitCount         = 6
	macExtendedDoubleSegmentDigitCount = 10
	macSingleSegmentDigitCount         = 12
	macExtendedSingleSegmentDigitCount = 16
	ipv6SingleSegmentDigitCount        = 32
	ipv6BinarySingleSegmentDigitCount  = 128
	ipv4BinarySingleSegmentDigitCount  = 32
	ipv6Base85SingleSegmentDigitCount  = 20
	maxWildcards                       = ipv6Base85SingleSegmentDigitCount - 1 // 20 wildcards is equivalent to a base 85 address
	ipv4SingleSegmentOctalDigitCount   = 11
	longHexDigits                      = longSize >> 2
	longBinaryDigits                   = longSize
)

var (
	macMaxTriple uint64 = (MACMaxValuePerSegment << uint64(MACBitsPerSegment*2)) |
		(MACMaxValuePerSegment << uint64(MACBitsPerSegment)) | MACMaxValuePerSegment
	macMaxQuintuple = (macMaxTriple << uint64(MACBitsPerSegment*2)) | uint64(macMaxTriple>>uint64(MACBitsPerSegment))
)

func isSingleSegmentIPv6(
	str string,
	totalDigits int,
	isRange bool,
	frontTotalDigits int,
	ipv6SpecificOptions addrstrparam.IPv6AddressStringParams) (isSingle bool, err addrerr.AddressStringError) {
	backIsIpv6 := totalDigits == ipv6SingleSegmentDigitCount || // 32 hex chars with or without 0x
		(ipv6SpecificOptions.AllowsBinary() && totalDigits == ipv6BinarySingleSegmentDigitCount+2) || // 128 binary chars with 0b
		(isRange && totalDigits == 0 && (frontTotalDigits == ipv6SingleSegmentDigitCount ||
			(ipv6SpecificOptions.AllowsBinary() && frontTotalDigits == ipv6BinarySingleSegmentDigitCount+2)))
	if backIsIpv6 && isRange && totalDigits != 0 {
		frontIsIpv6 := frontTotalDigits == ipv6SingleSegmentDigitCount ||
			(ipv6SpecificOptions.AllowsBinary() && frontTotalDigits == ipv6BinarySingleSegmentDigitCount+2) ||
			frontTotalDigits == 0
		if !frontIsIpv6 {
			err = &addressStringError{addressError{str: str, key: "ipaddress.error.too.few.segments.digit.count"}}
			return
		}
	}
	isSingle = backIsIpv6
	return
}

// When checking for binary single segment, we must check for the exact number of digits for IPv4.
// This is because of ambiguity between IPv6 hex 32 chars starting with 0b and 0b before 30 binary chars.
// So we must therefore avoid 0b before 30 binary chars for IPv4.  We must require 0b before 32 binary chars.
// This only applies to single-segment.
// For segmented IPv4, there is no ambiguity and we allow binary segments of varying lengths,
// just like we do for inet_aton.

func isSingleSegmentIPv4(
	str string,
	nonZeroDigits,
	totalDigits int,
	isRange bool,
	frontNonZeroDigits,
	frontTotalDigits int,
	ipv4SpecificOptions addrstrparam.IPv4AddressStringParams) (isSingle bool, err addrerr.AddressStringError) {
	backIsIpv4 := nonZeroDigits <= ipv4SingleSegmentOctalDigitCount ||
		(ipv4SpecificOptions.AllowsBinary() && totalDigits == ipv4BinarySingleSegmentDigitCount+2) ||
		(isRange && totalDigits == 0 && (frontTotalDigits <= ipv4SingleSegmentOctalDigitCount ||
			(ipv4SpecificOptions.AllowsBinary() && frontTotalDigits == ipv4BinarySingleSegmentDigitCount+2)))
	if backIsIpv4 && isRange && totalDigits != 0 {
		frontIsIpv4 := frontNonZeroDigits <= ipv4SingleSegmentOctalDigitCount ||
			(ipv4SpecificOptions.AllowsBinary() && frontTotalDigits == ipv4BinarySingleSegmentDigitCount+2) ||
			frontTotalDigits == 0
		if !frontIsIpv4 {
			err = &addressStringError{addressError{str: str, key: "ipaddress.error.too.few.segments.digit.count"}}
			return
		}
	}
	isSingle = backIsIpv4
	return
}

type strValidator struct{}

func (strValidator) validateIPAddressStr(fromString *IPAddressString, validationOptions addrstrparam.IPAddressStringParams) (prov ipAddressProvider, err addrerr.AddressStringError) {
	str := fromString.str
	pa := parsedIPAddress{
		originator:         fromString,
		options:            validationOptions,
		ipAddressParseData: ipAddressParseData{addressParseData: addressParseData{str: str}},
	}
	if err = validateIPAddress(validationOptions, str, 0, len(str), pa.getIPAddressParseData(), false); err == nil {
		if err = parseAddressQualifier(str, validationOptions, nil, pa.getIPAddressParseData(), len(str)); err == nil {
			prov, err = chooseIPAddressProvider(fromString, str, validationOptions, &pa)
		} else {
			prov = getInvalidProvider(validationOptions)
		}
	} else {
		prov = getInvalidProvider(validationOptions)
	}
	return
}

func getInvalidProvider(validationOptions addrstrparam.IPAddressStringParams) ipAddressProvider {
	if validationOptions == defaultIPAddrParameters {
		return invalidProvider
	}
	return &nullProvider{isInvalidVal: true, ipType: invalidType, params: validationOptions}
}

func (strValidator) validateMACAddressStr(fromString *MACAddressString, validationOptions addrstrparam.MACAddressStringParams) (prov macAddressProvider, err addrerr.AddressStringError) {
	str := fromString.str
	pa := parsedMACAddress{
		originator:          fromString,
		macAddressParseData: macAddressParseData{addressParseData: addressParseData{str: str}},
		params:              validationOptions,
		creationLock:        &sync.Mutex{},
	}
	if err = validateMACAddress(validationOptions, str, 0, len(str), pa.getMACAddressParseData()); err == nil {
		addressParseData := pa.getAddressParseData()
		prov, err = chooseMACAddressProvider(fromString, validationOptions, &pa, addressParseData)
	} else {
		prov = getInvalidMACProvider(validationOptions)
	}
	if err != nil && prov == nil {
		prov = getInvalidMACProvider(validationOptions)
	}
	return
}

func getInvalidMACProvider(validationOptions addrstrparam.MACAddressStringParams) macAddressProvider {
	if validationOptions == defaultMACAddrParameters {
		return invalidMACProvider
	}
	return macAddressNullProvider{validationOptions}
}

func validateIPAddress(
	validationOptions addrstrparam.IPAddressStringParams,
	str string,
	strStartIndex, strEndIndex int,
	parseData *ipAddressParseData,
	isEmbeddedIPv4 bool) addrerr.AddressStringError {
	return validateAddress(validationOptions, nil, str, strStartIndex, strEndIndex, parseData, nil, isEmbeddedIPv4)
}

func validateMACAddress(
	validationOptions addrstrparam.MACAddressStringParams,
	str string,
	strStartIndex, strEndIndex int,
	parseData *macAddressParseData) addrerr.AddressStringError {
	return validateAddress(nil, validationOptions, str, strStartIndex, strEndIndex, nil, parseData, false)
}

/**
* This method is the mega-parser.
* It is designed to go through the characters one-by-one as a big if/else.
* You have basically several cases: digits, segment separators (. : -), end characters like zone or prefix length,
* range characters denoting a range a-b, wildcard char *, and the 'x' character used to denote hex like 0xf.
*
* Most of the processing occurs in the segment characters, where each segment is analyzed based on what chars came before.
*
* We can parse all possible imaginable variations of mac, ipv4, and ipv6.
*
* This is not the clearest way to write such a parser, because the code for each possible variation is interspersed amongst the various cases,
* so you cannot easily see the code for a given variation clearly, but written this way it may be the fastest parser since we basically account
* for all possibilities simultaneously as we move through the characters just once.
*
 */
func validateAddress(
	validationOptions addrstrparam.IPAddressStringParams,
	macOptions addrstrparam.MACAddressStringParams,
	str string,
	strStartIndex, strEndIndex int,
	ipParseData *ipAddressParseData,
	macParseData *macAddressParseData,
	isEmbeddedIPv4 bool) addrerr.AddressStringError {

	isMac := macParseData != nil

	var parseData *addressParseData
	var stringFormatParams addrstrparam.AddressStringFormatParams
	var ipv6SpecificOptions addrstrparam.IPv6AddressStringParams
	var ipv4SpecificOptions addrstrparam.IPv4AddressStringParams
	var macSpecificOptions addrstrparam.MACAddressStringFormatParams
	var baseOptions addrstrparam.AddressStringParams
	var macFormat macFormat
	canBeBase85 := false
	if isMac {
		baseOptions = macOptions
		macSpecificOptions = macOptions.GetFormatParams()
		stringFormatParams = macSpecificOptions
		macParseData.init(str)
		parseData = macParseData.getAddressParseData()
	} else {
		baseOptions = validationOptions
		// we set stringFormatParams when we know what ip version we have
		ipParseData.init(str)
		parseData = ipParseData.getAddressParseData()
		ipv6SpecificOptions = validationOptions.GetIPv6Params()
		canBeBase85 = ipv6SpecificOptions.AllowsBase85()
		ipv4SpecificOptions = validationOptions.GetIPv4Params()
	}

	index := strStartIndex

	// per segment variables
	var frontDigitCount, frontLeadingZeroCount, frontSingleWildcardCount, leadingZeroCount,
		singleWildcardCount, wildcardCount, frontWildcardCount int

	var extendedCharacterIndex, extendedRangeWildcardIndex, rangeWildcardIndex,
		hexDelimiterIndex, frontHexDelimiterIndex, segmentStartIndex,
		segmentValueStartIndex = -1, -1, -1, -1, -1, index, index

	var isSegmented, leadingWithZero, hasDigits, frontIsStandardRangeChar, atEnd,
		firstSegmentDashedRange, frontUppercase, uppercase,
		isSingleIPv6, isSingleSegment, isDoubleSegment bool

	var err addrerr.AddressStringError

	checkCharCounts := true

	var version IPVersion
	var currentValueHex, currentFrontValueHex, extendedValue uint64

	charArray := chars

	var currentChar byte
	for {
		if index >= strEndIndex {
			// for ipv6 or ipv4 we set current char to be the right separator to parse the last segment,
			// so that is why we have the check here for index == strEndIndex and not index >= strEndIndex
			atEnd = index == strEndIndex
			if atEnd {
				parseData.setAddressEndIndex(index)
				if isSegmented {
					if isMac {
						currentChar = byte(*macFormat)
						isDoubleSegment = parseData.getSegmentCount() == 1 && currentChar == RangeSeparator
						macParseData.setDoubleSegment(isDoubleSegment)
						if isDoubleSegment {
							totalDigits := index - segmentValueStartIndex
							macParseData.setExtended(totalDigits == macExtendedDoubleSegmentDigitCount)
						}
					} else {
						// we are not base 85, so error if necessary
						if extendedCharacterIndex >= 0 {
							return &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
								extendedCharacterIndex}
						}
						//current char is either . or : to handle last segment, unless we have double :: in which case we already handled last segment
						if version.IsIPv4() {
							currentChar = IPv4SegmentSeparator
						} else { //ipv6
							if index == segmentStartIndex {
								if index == parseData.getConsecutiveSeparatorIndex()+2 {
									//ends with ::, we've already parsed the last segment
									break
								}
								return &addressStringError{addressError{str: str, key: "ipaddress.error.cannot.end.with.single.separator"}}
							} else if ipParseData.isProvidingMixedIPv6() {
								//no need to parse the last segment, since it is mixed we already have
								break
							} else {
								currentChar = IPv6SegmentSeparator
							}
						}
					}
				} else {
					// no segment separator so far and segmentCount is 0
					// it could be all addresses like "*", empty "", prefix-only ip address like /64, single segment like 12345, or single segment range like 12345-67890
					totalCharacterCount := index - strStartIndex
					if totalCharacterCount == 0 {
						//it is ""
						if !isMac && ipParseData.hasPrefixSeparator() {
							//if !validationOptions.AllowsPrefixOnly() {
							return &addressStringError{addressError{str: str, key: "ipaddress.error.prefix.only"}}
							//}
						} else if !baseOptions.AllowsEmpty() {
							return &addressStringError{addressError{str: str, key: "ipaddress.error.empty"}}
						}
						parseData.setEmpty(true)
						break
					} else if wildcardCount == totalCharacterCount && wildcardCount <= maxWildcards { //20 wildcards are base 85!
						if !baseOptions.AllowsAll() {
							return &addressStringError{addressError{str: str, key: "ipaddress.error.all"}}
						}
						parseData.setHasWildcard()
						parseData.setAll()
						break
					}
					// At this point it is single segment like 12345 or single segment range like 12345-67890
					totalDigits := index - segmentValueStartIndex
					frontTotalDigits := frontLeadingZeroCount + frontDigitCount
					if isMac {
						// we handle the double segment format abcdef-abcdef here
						isDoubleSeg := (totalDigits == macDoubleSegmentDigitCount || totalDigits == macExtendedDoubleSegmentDigitCount) &&
							(frontTotalDigits == macDoubleSegmentDigitCount || frontWildcardCount > 0)
						isDoubleSeg = isDoubleSeg || (frontTotalDigits == macDoubleSegmentDigitCount && wildcardCount > 0)
						isDoubleSeg = isDoubleSeg || (frontWildcardCount > 0 && wildcardCount > 0)
						if isDoubleSeg && !firstSegmentDashedRange { //checks for *-abcdef and abcdef-* and abcdef-abcdef and *-* two segment addresses
							// firstSegmentDashedRange means that the range character is '|'
							addressSize := macOptions.GetPreferredLen()
							if addressSize == addrstrparam.EUI64Len && totalDigits == macDoubleSegmentDigitCount {
								return &addressStringError{addressError{str: str, key: "ipaddress.error.too.few.segments"}}
							} else if addressSize == addrstrparam.MAC48Len && totalDigits == macExtendedDoubleSegmentDigitCount {
								return &addressStringError{addressError{str: str, key: "ipaddress.error.too.many.segments"}}
							}
							// we have aaaaaa-bbbbbb
							if !macOptions.AllowsSingleDashed() {
								return &addressStringError{addressError{str: str, key: "ipaddress.mac.error.format"}}
							}
							isDoubleSegment = true
							macParseData.setDoubleSegment(true)
							macParseData.setExtended(totalDigits == macExtendedDoubleSegmentDigitCount) //we have aaaaaa-bbbbbbbbbb
							currentChar = MACDashSegmentSeparator
							checkCharCounts = false //counted chars already
						} else if frontWildcardCount > 0 || wildcardCount > 0 {
							// either x-* or *-x, we treat these as if they can be expanded to x-*-*-*-*-* or *-*-*-*-*-x
							if !macOptions.AllowsSingleDashed() {
								return &addressStringError{addressError{str: str, key: "ipaddress.mac.error.format"}}
							}
							currentChar = MACDashSegmentSeparator
						} else {
							// a string of digits with no segment separator
							// here we handle abcdefabcdef or abcdefabcdef|abcdefabcdef or abcdefabcdef-abcdefabcdef
							if !baseOptions.AllowsSingleSegment() {
								return &addressStringError{addressError{str: str, key: "ipaddress.error.single.segment"}}
							}
							is12Digits := totalDigits == macSingleSegmentDigitCount
							is16Digits := totalDigits == macExtendedSingleSegmentDigitCount
							isNoDigits := totalDigits == 0
							if is12Digits || is16Digits || isNoDigits {
								var frontIs12Digits, frontIs16Digits, frontIsNoDigits bool
								if rangeWildcardIndex >= 0 {
									frontIs12Digits = frontTotalDigits == macSingleSegmentDigitCount
									frontIs16Digits = frontTotalDigits == macExtendedSingleSegmentDigitCount
									frontIsNoDigits = frontTotalDigits == 0
									if is12Digits {
										if !frontIs12Digits && !frontIsNoDigits {
											return &addressStringError{addressError{str: str, key: "ipaddress.error.front.digit.count"}}
										}
									} else if is16Digits {
										if !frontIs16Digits && !frontIsNoDigits {
											return &addressStringError{addressError{str: str, key: "ipaddress.error.front.digit.count"}}
										}
									} else if isNoDigits {
										if !frontIs12Digits && !frontIs16Digits {
											return &addressStringError{addressError{str: str, key: "ipaddress.error.front.digit.count"}}
										}
									}
								} else if isNoDigits {
									return &addressStringError{addressError{str: str, key: "ipaddress.error.too.few.segments.digit.count"}}
								}
								isSingleSegment = true
								parseData.setSingleSegment()
								macParseData.setExtended(is16Digits || frontIs16Digits)
								currentChar = MACColonSegmentSeparator
								checkCharCounts = false //counted chars already
							} else {
								return &addressStringError{addressError{str: str, key: "ipaddress.error.too.few.segments.digit.count"}}
							}
						}
					} else {
						//a string of digits with no segment separator
						if !baseOptions.AllowsSingleSegment() {
							return &addressStringError{addressError{str: str, key: "ipaddress.error.single.segment"}}
						}

						isRange := rangeWildcardIndex >= 0
						isSingleSeg, serr := isSingleSegmentIPv6(str, totalDigits, isRange, frontTotalDigits, ipv6SpecificOptions)
						if serr != nil {
							return serr
						} else if isSingleSeg {
							// we are not base 85, so error if necessary
							if extendedCharacterIndex >= 0 {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
									extendedCharacterIndex,
								}
							}
							isSingleIPv6 = true
							currentChar = IPv6SegmentSeparator
						} else {
							if canBeBase85 {
								if canBeBase85, serr = parseBase85(
									validationOptions, str, strStartIndex, strEndIndex, ipParseData,
									extendedRangeWildcardIndex, totalCharacterCount, index); canBeBase85 {
									break
								}
								if serr != nil {
									return serr
								}
							}
							leadingZeros := leadingZeroCount
							if leadingWithZero {
								leadingZeros++
							}
							isSingleSeg, serr = isSingleSegmentIPv4(
								str,
								totalDigits-leadingZeros,
								totalDigits,
								isRange,
								frontDigitCount,
								frontTotalDigits,
								ipv4SpecificOptions)
							if serr != nil {
								return serr
							} else if isSingleSeg {
								// we are not base 85, so error if necessary
								if extendedCharacterIndex >= 0 {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
										extendedCharacterIndex,
									}
								}
								currentChar = IPv4SegmentSeparator
							} else {
								return &addressStringError{addressError{str: str, key: "ipaddress.error.too.few.segments.digit.count"}}
							}
						}
						isSingleSegment = true
						parseData.setSingleSegment()
						checkCharCounts = false // counted chars already
					}
				}
			} else {
				break
			}
		} else {
			currentChar = str[index]
		}

		// evaluate the character
		if currentChar <= '9' && currentChar >= '0' {
			if hasDigits {
				currentValueHex = currentValueHex<<4 | uint64(charArray[currentChar])
			} else {
				if currentChar == '0' {
					if leadingWithZero {
						leadingZeroCount++
					} else {
						leadingWithZero = true
					}
				} else {
					hasDigits = true
					currentValueHex = currentValueHex<<4 | uint64(charArray[currentChar])
				}
			}
			index++
		} else if currentChar >= 'a' && currentChar <= 'f' {
			currentValueHex = currentValueHex<<4 | uint64(charArray[currentChar])
			hasDigits = true
			index++
		} else if currentChar == IPv4SegmentSeparator {
			segCount := parseData.getSegmentCount()
			// could be mac or ipv4, we handle either one
			if isMac {
				if segCount == 0 {
					if !macOptions.AllowsDotted() {
						return &addressStringError{addressError{str: str, key: "ipaddress.mac.error.format"}}
					}
					macFormat = dotted
					macParseData.setFormat(macFormat)
					parseData.initSegmentData(MediaAccessControlDotted64SegmentCount)
					isSegmented = true
				} else {
					if macFormat != dotted {
						return &addressStringIndexError{
							addressStringError{addressError{str: str, key: "ipaddress.mac.error.mix.format.characters.at.index"}},
							index}
					}
					var limit int
					if macOptions.GetPreferredLen() == addrstrparam.MAC48Len {
						limit = MediaAccessControlDottedSegmentCount
					} else {
						limit = MediaAccessControlDotted64SegmentCount
					}
					if segCount >= limit {
						return &addressStringError{addressError{str: str, key: "ipaddress.error.too.many.segments"}}
					}
				}
			} else {
				//end of an ipv4 segment
				if segCount == 0 {
					if !validationOptions.AllowsIPv4() {
						return &addressStringError{addressError{str: str, key: "ipaddress.error.ipv4"}}
					}
					version = IPv4
					ipParseData.setVersion(version)
					stringFormatParams = ipv4SpecificOptions
					canBeBase85 = false
					parseData.initSegmentData(IPv4SegmentCount)
					isSegmented = true
				} else if ipParseData.getProviderIPVersion().IsIPv6() {
					//mixed IPv6 address like 1:2:3:4:5:6:1.2.3.4
					if !ipv6SpecificOptions.AllowsMixed() {
						return &addressStringError{addressError{str: str, key: "ipaddress.error.no.mixed"}}
					}
					totalSegmentCount := segCount + IPv6MixedReplacedSegmentCount
					if totalSegmentCount > IPv6SegmentCount {
						return &addressStringError{addressError{str: str, key: "ipaddress.error.too.many.segments"}}
					}
					if wildcardCount > 0 {
						if parseData.getConsecutiveSeparatorIndex() < 0 &&
							totalSegmentCount < IPv6SegmentCount &&
							ipv6SpecificOptions.AllowsWildcardedSeparator() {
							// the '*' is covering an additional ipv6 segment (eg 1:2:3:4:5:*.2.3.4, the * covers both an ipv4 and ipv6 segment)
							// we flag this IPv6 segment with keyMergedMixed
							parseData.setHasWildcard()
							assign6Attributes2Values1Flags(segmentStartIndex, index, segmentStartIndex, segmentStartIndex, index, segmentStartIndex,
								parseData, segCount, 0, IPv6MaxValuePerSegment, keyWildcard|keyMergedMixed)
							parseData.incrementSegmentCount()
						}
					}
					mixedOptions := ipv6SpecificOptions.GetMixedParams()
					pa := &parsedIPAddress{
						ipAddressParseData: ipAddressParseData{addressParseData: addressParseData{str: str}},
						options:            mixedOptions,
					}
					err = validateIPAddress(mixedOptions, str, segmentStartIndex, strEndIndex, &pa.ipAddressParseData, true)
					if err != nil {
						return err
					}
					pa.clearQualifier()
					err = checkSegments(str, mixedOptions, pa.getIPAddressParseData())
					if err != nil {
						return err
					}
					ipParseData.setMixedParsedAddress(pa)
					index = pa.getAddressParseData().getAddressEndIndex()
					continue
				} else if segCount >= IPv4SegmentCount {
					return &addressStringError{addressError{str: str, key: "ipaddress.error.ipv4.too.many.segments"}}
				}
			}
			if wildcardCount > 0 {
				if !stringFormatParams.GetRangeParams().AllowsWildcard() {
					return &addressStringError{addressError{str: str, key: "ipaddress.error.no.wildcard"}}
				}
				//wildcards must appear alone
				totalDigits := index - segmentStartIndex
				if wildcardCount != totalDigits || hexDelimiterIndex >= 0 {
					return &addressStringIndexError{
						addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
						index}
				}
				parseData.setHasWildcard()
				startIndex := index - wildcardCount
				var max uint64
				if isMac {
					max = MACMaxValuePerDottedSegment
				} else {
					max = IPv4MaxValuePerSegment
				}
				assign6Attributes2Values1Flags(startIndex, index, startIndex, startIndex, index, startIndex,
					parseData, segCount, 0, max, keyWildcard)
				wildcardCount = 0
			} else {
				var flags, rangeFlags, radix uint32
				var value uint64
				digitStartIndex := segmentValueStartIndex + leadingZeroCount
				digitCount := index - digitStartIndex
				if leadingWithZero {
					if digitCount == 1 {
						if leadingZeroCount == 0 && rangeWildcardIndex < 0 && hexDelimiterIndex < 0 {
							// handles 0, but not 1-0 or 0x0
							assign4Attributes(digitStartIndex, index, parseData, segCount, 10, segmentValueStartIndex)
							parseData.incrementSegmentCount()
							index++
							segmentStartIndex = index
							segmentValueStartIndex = index
							leadingWithZero = false
							continue
						}
					} else {
						leadingZeroCount++
						digitStartIndex++
						digitCount--
					}
					leadingWithZero = false // reset this flag now that we've used it
				}
				noValuesToSet := false
				if digitCount == 0 {
					// we allow an empty range boundary to denote the max value
					if rangeWildcardIndex < 0 || hexDelimiterIndex >= 0 || !stringFormatParams.GetRangeParams().AllowsInferredBoundary() {
						// starts with '.', or has two consecutive '.'
						return &addressStringIndexError{
							addressStringError{addressError{str: str, key: "ipaddress.error.empty.segment.at.index"}},
							index}
					} else if isMac {
						value = MACMaxValuePerDottedSegment
						radix = MACDefaultTextualRadix
					} else {
						value = IPv4MaxValuePerSegment // for inet-aton multi-segment, this will be adjusted later
						radix = IPv4DefaultTextualRadix
					}
					rangeFlags = keyInferredUpperBoundary
				} else { // digitCount > 0
					// Note: we cannot do max value check on ipv4 until after all segments have been read due to inet_aton joined segments,
					// although we can do a preliminary check here that is in fact needed to prevent overflow when calculating values later
					isBinary := false
					hasLeadingZeros := leadingZeroCount > 0
					isSingleWildcard := singleWildcardCount > 0
					if isMac || hexDelimiterIndex >= 0 {
						if isMac { // mac dotted segments aabb.ccdd.eeff
							maxMacChars := 4
							if digitCount > maxMacChars { //
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
									segmentValueStartIndex}
							}
							totalDigits := digitCount + leadingZeroCount
							if hexDelimiterIndex >= 0 {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
									hexDelimiterIndex}
							} else if leadingZeroCount > 0 && !stringFormatParams.AllowsLeadingZeros() {
								return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
							} else if !stringFormatParams.AllowsUnlimitedLeadingZeros() && totalDigits > maxMacChars {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
									segmentValueStartIndex}
							} else if !macSpecificOptions.AllowsShortSegments() && totalDigits < maxMacChars {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.short.at.index"}},
									segmentValueStartIndex}
							}
						} else if !stringFormatParams.AllowsLeadingZeros() {
							// the '0' preceding the 'x' is not allowed
							return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
						} else if !ipv4SpecificOptions.Allows_inet_aton_hex() {
							return &addressStringError{addressError{str: str, key: "ipaddress.error.ipv4.segment.hex"}}
						} else if hasLeadingZeros && !ipv4SpecificOptions.Allows_inet_aton_leading_zeros() {
							// the '0' following the 'x' is not allowed
							return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
						} else {
							if digitCount > 8 { // 0xffffffff
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
									segmentValueStartIndex}
							}
							ipParseData.set_has_inet_aton_value(true)
						}
						radix = 16
						if isSingleWildcard {
							if rangeWildcardIndex >= 0 {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
									index}
							}
							err = assignSingleWildcard16(currentValueHex, str, digitStartIndex, index, singleWildcardCount, parseData, segCount, segmentValueStartIndex, stringFormatParams)
							if err != nil {
								return err
							}
							value = 0
							noValuesToSet = true
							singleWildcardCount = 0
						} else {
							value = currentValueHex
						}
						hexDelimiterIndex = -1
					} else {
						isBinaryOrOctal := hasLeadingZeros
						if isBinaryOrOctal {
							isBinary = ipv4SpecificOptions.AllowsBinary() && isBinaryDelimiter(str, digitStartIndex)
							isBinaryOrOctal = isBinary || ipv4SpecificOptions.Allows_inet_aton_octal()
						}
						if isBinaryOrOctal {
							if !stringFormatParams.AllowsLeadingZeros() {
								return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
							}
							if isBinary {
								if digitCount > 33 {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
										segmentValueStartIndex}
								}
								digitStartIndex++ // exclude the 'b' in 0b1100
								digitCount--      // exclude the 'b'
								radix = 2
								ipParseData.setHasBinaryDigits(true)
								if isSingleWildcard {
									if rangeWildcardIndex >= 0 {
										return &addressStringIndexError{
											addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
											index}
									}
									if digitCount > 16 {
										if parseErr := parseSingleSegmentSingleWildcard2(str, digitStartIndex, index, singleWildcardCount, parseData, segCount, segmentValueStartIndex, stringFormatParams); parseErr != nil {
											return parseErr
										}
									} else {
										if parseErr := switchSingleWildcard2(currentValueHex, str, digitStartIndex, index, singleWildcardCount, parseData, segCount, segmentValueStartIndex, stringFormatParams); parseErr != nil {
											return parseErr
										}
									}
									value = 0
									noValuesToSet = true
									singleWildcardCount = 0
								} else {
									if digitCount > 16 {
										value = parseLong2(str, digitStartIndex, index)
									} else {
										value, err = switchValue2(currentValueHex, str, digitCount)
										if err != nil {
											return err
										}
									}
								}
							} else {
								if leadingZeroCount > 1 && !ipv4SpecificOptions.Allows_inet_aton_leading_zeros() {
									return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
								} else if digitCount > 11 { //octal 037777777777
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
										segmentValueStartIndex}
								}
								ipParseData.set_has_inet_aton_value(true)
								radix = 8
								if isSingleWildcard {
									if rangeWildcardIndex >= 0 {
										return &addressStringIndexError{
											addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
											index}
									}
									if parseErr := switchSingleWildcard8(currentValueHex, str, digitStartIndex, index, singleWildcardCount, parseData, segCount, segmentValueStartIndex, stringFormatParams); parseErr != nil {
										return parseErr
									}
									value = 0
									noValuesToSet = true
									singleWildcardCount = 0
								} else {
									value, err = switchValue8(currentValueHex, str, digitCount)
									if err != nil {
										return err
									}
								}
							}
						} else {
							if hasLeadingZeros {
								if !stringFormatParams.AllowsLeadingZeros() {
									return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
								}
								ipParseData.setHasIPv4LeadingZeros(true)
							}
							if digitCount > 10 { // 4294967295
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
									segmentValueStartIndex}
							}
							radix = 10
							if isSingleWildcard {
								if rangeWildcardIndex >= 0 {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
										index}
								}
								if parseErr := switchSingleWildcard10(currentValueHex, str, digitStartIndex, index, singleWildcardCount, parseData, segCount, segmentValueStartIndex, ipv4SpecificOptions); parseErr != nil {
									return parseErr
								}
								value = 0
								noValuesToSet = true
								singleWildcardCount = 0
							} else {
								value, err = switchValue10(currentValueHex, str, digitCount)
								if err != nil {
									return err
								}
								flags = keyStandardStr
							}
						}
					}
					hasDigits = false
					currentValueHex = 0
				}
				if rangeWildcardIndex >= 0 {
					var frontRadix uint32
					var front uint64
					frontStartIndex := rangeWildcardIndex - frontDigitCount
					frontEndIndex := rangeWildcardIndex
					frontLeadingZeroStartIndex := frontStartIndex - frontLeadingZeroCount
					if !stringFormatParams.GetRangeParams().AllowsRangeSeparator() {
						return &addressStringError{addressError{str: str, key: "ipaddress.error.no.range"}}
					} else if frontSingleWildcardCount > 0 || frontWildcardCount > 0 { //no wildcards in ranges
						return &addressStringIndexError{
							addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
							rangeWildcardIndex}
					}
					frontEmpty := frontStartIndex == frontEndIndex
					isReversed := false
					hasFrontLeadingZeros := frontLeadingZeroCount > 0
					if isMac || frontHexDelimiterIndex >= 0 {
						if isMac {
							totalFrontDigits := frontDigitCount + frontLeadingZeroCount
							maxMacChars := 4
							if frontHexDelimiterIndex >= 0 {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
									frontHexDelimiterIndex}
							} else if hasFrontLeadingZeros && !stringFormatParams.AllowsLeadingZeros() {
								return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
							} else if !stringFormatParams.AllowsUnlimitedLeadingZeros() && totalFrontDigits > maxMacChars {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
									frontLeadingZeroStartIndex}
							} else if !macSpecificOptions.AllowsShortSegments() && totalFrontDigits < maxMacChars {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.short.at.index"}},
									frontLeadingZeroStartIndex}
							} else if frontEmpty { //we allow the front of a range to be empty in which case it is 0
								if !stringFormatParams.GetRangeParams().AllowsInferredBoundary() {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.empty.segment.at.index"}},
										index}
								}
								rangeFlags |= keyInferredLowerBoundary
								front = 0
							} else if frontDigitCount > maxMacChars { // mac dotted segments aaa.bbb.ccc.ddd
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
									frontLeadingZeroStartIndex}
							} else {
								front = currentFrontValueHex
								isReversed = front > value && digitCount != 0
							}
						} else if !stringFormatParams.AllowsLeadingZeros() {
							// the '0' preceding the 'x' is not allowed
							return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
						} else if !ipv4SpecificOptions.Allows_inet_aton_hex() {
							return &addressStringError{addressError{str: str, key: "ipaddress.error.ipv4.segment.hex"}}
						} else if hasFrontLeadingZeros && !ipv4SpecificOptions.Allows_inet_aton_leading_zeros() {
							// the '0' following the 'x' is not allowed
							return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
						} else if frontEmpty {
							return &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.empty.segment.at.index"}},
								index}
						} else if frontDigitCount > 8 { // 0xffffffff
							return &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
								frontLeadingZeroStartIndex}
						} else {
							ipParseData.set_has_inet_aton_value(true)
							front = currentFrontValueHex
							isReversed = front > value && digitCount != 0
						}
						frontRadix = 16
					} else {
						if hasFrontLeadingZeros {
							if !stringFormatParams.AllowsLeadingZeros() {
								return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
							}
							if ipv4SpecificOptions.AllowsBinary() && isBinaryDelimiter(str, frontStartIndex) {
								if frontDigitCount > 33 {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
										frontLeadingZeroStartIndex}
								}
								ipParseData.setHasBinaryDigits(true)
								frontStartIndex++
								frontDigitCount--
								if frontDigitCount > 16 {
									front = parseLong2(str, frontStartIndex, frontEndIndex)
								} else {
									front, err = switchValue2(currentFrontValueHex, str, frontDigitCount)
									if err != nil {
										return err
									}
								}
								frontRadix = 2
								isReversed = digitCount != 0 && front > value
							} else if ipv4SpecificOptions.Allows_inet_aton_octal() {
								if frontLeadingZeroCount > 1 && !ipv4SpecificOptions.Allows_inet_aton_leading_zeros() {
									return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
								} else if frontDigitCount > 11 { // 037777777777
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
										frontLeadingZeroStartIndex}
								}
								ipParseData.set_has_inet_aton_value(true)
								front, err = switchValue8(currentFrontValueHex, str, frontDigitCount)
								if err != nil {
									return err
								}
								frontRadix = 8
								isReversed = digitCount != 0 && front > value
							}
						}
						if frontRadix == 0 {
							frontRadix = 10
							if frontEmpty { //we allow the front of a range to be empty in which case it is 0
								if !stringFormatParams.GetRangeParams().AllowsInferredBoundary() {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.empty.segment.at.index"}},
										index}
								}
								rangeFlags |= keyInferredLowerBoundary
							} else if frontDigitCount > 10 { // 4294967295
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
									frontLeadingZeroStartIndex}
							} else {
								front, err = switchValue10(currentFrontValueHex, str, frontDigitCount)
								if err != nil {
									return err
								}
								if hasFrontLeadingZeros {
									if !stringFormatParams.AllowsLeadingZeros() {
										return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
									}
									ipParseData.setHasIPv4LeadingZeros(true)
								}
								isReversed = digitCount != 0 && front > value
								if !isReversed {
									if leadingZeroCount == 0 && (flags&keyStandardStr) != 0 {
										rangeFlags |= keyStandardRangeStr | keyStandardStr
									} else {
										rangeFlags |= keyStandardStr
									}
								}
							}
						}
					}
					backEndIndex := index
					if isReversed {
						if !stringFormatParams.GetRangeParams().AllowsReverseRange() {
							return &addressStringError{addressError{str: str, key: "ipaddress.error.invalidRange"}}
						}
						// switcheroo
						frontStartIndex, digitStartIndex = digitStartIndex, frontStartIndex
						frontEndIndex, backEndIndex = backEndIndex, frontEndIndex
						frontLeadingZeroStartIndex, segmentValueStartIndex = segmentValueStartIndex, frontLeadingZeroStartIndex
						frontRadix, radix = radix, frontRadix
						front, value = value, front
					}
					assign6Attributes2Values2Flags(frontStartIndex, frontEndIndex, frontLeadingZeroStartIndex, digitStartIndex, backEndIndex, segmentValueStartIndex,
						parseData, segCount, front, value, rangeFlags|keyRangeWildcard|frontRadix, radix)
					rangeWildcardIndex = -1
				} else if !noValuesToSet {
					assign3Attributes1Values1Flags(digitStartIndex, index, segmentValueStartIndex, parseData, segCount, value, flags|radix)
				}
				leadingZeroCount = 0
			}
			parseData.incrementSegmentCount()
			index++
			segmentValueStartIndex = index
			segmentStartIndex = index
			// end of IPv4 segments and mac segments with '.' separators
		} else {
			//checking for all IPv6 and MAC48Len segments, as well as the front range of all segments IPv4, IPv6, and MAC48Len
			//the range character '-' is the same as one of the separators '-' for MAC48Len,
			//so further work is required to distinguish between the front of IPv6/IPv4/MAC48Len range and MAC48Len segment
			//we also handle IPv6 segment and MAC48Len segment in the same place to avoid code duplication
			var isSpace, isDashedRangeChar, isRangeChar bool
			if currentChar == IPv6SegmentSeparator {
				isRangeChar = false
				isSpace = false
			} else {
				isRangeChar = currentChar == RangeSeparator
				if isRangeChar || (isMac && (currentChar == MacDashedSegmentRangeSeparator)) {
					isSpace = false
					isDashedRangeChar = !isRangeChar

					/*
					 There are 3 cases here, A, B and C.
					 A - we have two MAC48Len segments a-b-
					 B - we have the front of a range segment, either a-b which is MAC48Len or ipv6AddrType,  or a|b or a<space>b which is MAC48Len
					 C - we have a single segment, either a MAC48Len segment a- or an IPv6 or MAC48Len segment a:
					*/

					/*
					 Here we have either a '-' or '|' character or a space ' '

					 If we have a '-' character:
					 For MAC48Len address, the cases are:
					 1. we did not previously set macFormat and we did not previously encounter '|'
					 		-if rangeWildcardIndex >= 0 we have dashed a-b- we treat as two segments, case A (we cannot have a|b because that would have set macFormat previously)
					 		-if rangeWildcardIndex < 0, we treat as front of range, case B, later we will know for sure if really front of range
					 2. we previously set macFormat or we previously encountered '|'
					 		if set to dashed we treat as one segment, may or may not be range segment, case C
					 		if we previously encountered '|' we treat as dashed range segment, case C
					 		if not we treat as front of range, case B

					 For IPv6, this is always front of range, case B

					 If we have a '|' character, we have front of range MAC48Len, case B
					*/
					// we know either isRangeChar or isDashedRangeChar is true at this point
					endOfHexSegment := false
					if isMac {
						if macFormat == nil {
							if rangeWildcardIndex >= 0 && !firstSegmentDashedRange {

								//case A, we have two segments a-b- or a-b|

								//we handle the first segment here, we handle the second segment in the usual place below
								if frontHexDelimiterIndex >= 0 {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
										frontHexDelimiterIndex}
								} else if hexDelimiterIndex >= 0 {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
										hexDelimiterIndex}
								} else if !macOptions.AllowsDashed() {
									return &addressStringError{addressError{str: str, key: "ipaddress.mac.error.format"}}
								}
								macFormat = dashed
								macParseData.setFormat(macFormat)
								checkCharCounts = false //counting chars later
								parseData.initSegmentData(ExtendedUniqueIdentifier64SegmentCount)
								isSegmented = true
								if frontWildcardCount > 0 {
									if !stringFormatParams.GetRangeParams().AllowsWildcard() {
										return &addressStringError{addressError{str: str, key: "ipaddress.error.no.wildcard"}}
									} else if frontSingleWildcardCount > 0 || frontLeadingZeroCount > 0 || frontDigitCount > 0 || frontHexDelimiterIndex >= 0 { //wildcards must appear alone
										return &addressStringIndexError{
											addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
											rangeWildcardIndex}
									}
									parseData.setHasWildcard()
									backDigits := index - segmentValueStartIndex
									var upperValue uint64
									if isDoubleSegment || backDigits == macDoubleSegmentDigitCount {
										//even when not already identified as a double segment address, which is something we can see
										//only when we reach the end of the address, we may have a-b| where a is * and b is a 6 digit value.
										//Here we are considering the max value of a.
										//If b is 6 digits, we need to consider the max value of * as if we know already it will be double segment.
										//We can do this because the max values will be checked after the address has been parsed,
										//so even if a-b| ends up being a full address a-b|c-d-e-f-a and not a-b|c,
										//the fact that we have 6 digits here will invalidate the first address,
										//so we can safely assume that this address must be a double segment a-b|c even before we have seen that.
										upperValue = macMaxTriple
									} else {
										upperValue = MACMaxValuePerSegment
									}
									startIndex := rangeWildcardIndex - frontWildcardCount
									assign6Attributes2Values1Flags(startIndex, rangeWildcardIndex, startIndex, startIndex, rangeWildcardIndex, startIndex,
										parseData, 0, 0, upperValue, keyWildcard)
								} else {
									if !stringFormatParams.AllowsLeadingZeros() && frontLeadingZeroCount > 0 {
										return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
									}
									startIndex := rangeWildcardIndex - frontDigitCount
									leadingZeroStartIndex := startIndex - frontLeadingZeroCount
									if frontSingleWildcardCount > 0 {
										if parseErr := assignSingleWildcard16(currentFrontValueHex, str, startIndex, rangeWildcardIndex, singleWildcardCount, parseData, 0, leadingZeroStartIndex, stringFormatParams); parseErr != nil {
											return parseErr
										}
									} else {
										var flags uint32
										if !frontUppercase {
											if frontDigitCount == 0 {
												return &addressStringIndexError{
													addressStringError{addressError{str: str, key: "ipaddress.error.empty.segment.at.index"}},
													startIndex}
											}
											flags = keyStandardStr
										}
										assign3Attributes1Values1Flags(startIndex, rangeWildcardIndex, leadingZeroStartIndex, parseData, 0, currentFrontValueHex, flags)
									}
								}
								segmentValueStartIndex = rangeWildcardIndex + 1
								segmentStartIndex = segmentValueStartIndex
								rangeWildcardIndex = -1
								parseData.incrementSegmentCount()
								//end of handling the first segment a- in a-b-
								//below we handle b- by setting endOfSegment here
								endOfHexSegment = isRangeChar
							} else { //we will treat this as the front of a range
								if isDashedRangeChar {
									firstSegmentDashedRange = true
								} else {
									endOfHexSegment = firstSegmentDashedRange
								}
							}
						} else {
							if macFormat == dashed {
								endOfHexSegment = isRangeChar
							} else if isDashedRangeChar {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
									index}
							}
						}
					}
					if !endOfHexSegment {
						if extendedCharacterIndex < 0 {
							//case B
							if rangeWildcardIndex >= 0 {
								if canBeBase85 {
									index++
									extendedCharacterIndex = index
								} else {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
										index}
								}
							} else {
								//here is where we handle the front 'a' of a range like 'a-b'
								rangeWildcardIndex = index

								frontIsStandardRangeChar = isRangeChar
								frontDigitCount = ((index - segmentValueStartIndex) - leadingZeroCount) - wildcardCount
								frontLeadingZeroCount = leadingZeroCount
								if leadingWithZero {
									if frontDigitCount != 1 {
										frontLeadingZeroCount++
										frontDigitCount--
									}
								}
								frontUppercase = uppercase
								frontHexDelimiterIndex = hexDelimiterIndex
								frontWildcardCount = wildcardCount
								frontSingleWildcardCount = singleWildcardCount
								currentFrontValueHex = currentValueHex

								index++
								segmentValueStartIndex = index
								hasDigits = false
								uppercase = false
								leadingWithZero = false
								hexDelimiterIndex = -1
								leadingZeroCount = 0
								wildcardCount = 0
								singleWildcardCount = 0
								currentValueHex = 0
							}
						} else {
							index++
						}
						continue
					}
				} else if isMac && currentChar == space {
					isSpace = true
				} else {
					// other characters handled here
					isZoneChar := false
					if currentChar == PrefixLenSeparator {
						if isMac {
							return &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
								index}
						}
						strEndIndex = index
						ipParseData.setHasPrefixSeparator(true)
						ipParseData.setQualifierIndex(index + 1)
					} else if currentChar >= 'A' && currentChar <= 'F' { // this is not paired with 'a' to 'f' because these are not canonical and hence not part of the fast path
						index++
						currentValueHex = (currentValueHex << 4) | uint64(charArray[currentChar])
						hasDigits = true
						uppercase = true
					} else {
						isSegWildcard := currentChar == SegmentWildcard
						if !isSegWildcard {
							isZoneChar = currentChar == SegmentSqlWildcard
							isSegWildcard = isZoneChar
						}
						if isSegWildcard {
							//the character * is always treated as wildcard (but later can be determined to be a base 85 character)

							//the character % denotes a zone and is also a character for the SQL wildcard,
							//and it is also a base 85 character,
							//so we treat it as zone only if the options allow it and it is in the zone position.
							//Either we have seen an ipv6 segment separator, or we are at the end of the correct number of digits for ipv6 single segment (which rules out base 85 or ipv4 single segment),
							//or we are the '*' all wildcard so far which can represent everything including ipv6
							//
							//In all other cases, the character is treated as wildcard,
							//but as is the case of other characters we may later discover we are base 85 ipv6
							//For base 85, we decided that having the same character mean two different thing depending on position in the string, that is not reasonable.
							//In fact, if the zone character were allowed, can you tell if there is a zone here or not: %%%%%%%%%%%%%%%%%%%%%%

							canBeZone := isZoneChar &&
								!isMac &&
								ipv6SpecificOptions.AllowsZone()
							if canBeZone {
								isIPv6 := parseData.getSegmentCount() > 0 && (isEmbeddedIPv4 || ipParseData.getProviderIPVersion() == IPv6) /* at end of IPv6 regular or mixed */
								if !isIPv6 {
									isIPv6, _ = isSingleSegmentIPv6(str, index-segmentValueStartIndex, rangeWildcardIndex >= 0, frontLeadingZeroCount+frontDigitCount, ipv6SpecificOptions)
									if !isIPv6 {
										isIPv6 = wildcardCount == index && wildcardCount <= maxWildcards /* all wildcards so far */
									}
								}
								canBeZone = isIPv6
							}
							if canBeZone {
								//we are not base 85
								canBeBase85 = false
								strEndIndex = index
								ipParseData.setZoned(true)
								ipParseData.setQualifierIndex(index + 1)
							} else {
								wildcardCount++
								index++
							}
						} else if currentChar == SegmentSqlSingleWildcard {
							hasDigits = true
							index++
							singleWildcardCount++
						} else if isHexDelimiter(currentChar) {
							if hasDigits || !leadingWithZero || leadingZeroCount > 0 || hexDelimiterIndex >= 0 || singleWildcardCount > 0 {
								if canBeBase85 {
									if extendedCharacterIndex < 0 {
										extendedCharacterIndex = index
									}
									index++
								} else {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
										index}
								}
							} else {
								if isMac {
									if parseData.getSegmentCount() > 0 {
										return &addressStringIndexError{
											addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
											index}
									}
								} else if version.IsIPv6() {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
										index}
								}
								hexDelimiterIndex = index
								leadingWithZero = false
								index++
								segmentValueStartIndex = index
							}
							//the remaining possibilities are base85 only
						} else if currentChar == AlternativeRangeSeparatorStr[0] {
							if index+1 == strEndIndex {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
									index}
							}
							currentChar = str[index+1]
							if currentChar == AlternativeRangeSeparatorStr[1] {
								if canBeBase85 {
									if extendedCharacterIndex < 0 {
										extendedCharacterIndex = index
									} else if extendedRangeWildcardIndex >= 0 {
										return &addressStringIndexError{
											addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
											index}
									}
									extendedRangeWildcardIndex = index
								} else {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
										index}
								}
								index += 2
							} else if currentChar == IPv6AlternativeZoneSeparatorStr[1] {
								if canBeBase85 && !isMac && ipv6SpecificOptions.AllowsZone() {
									strEndIndex = index
									ipParseData.setZoned(true)
									ipParseData.setBase85Zoned(true)
									ipParseData.setQualifierIndex(index + 2)
								} else {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
										index}
								}
							} else {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
									index - 1}
							}
						} else {
							if canBeBase85 {
								if currentChar < 0 || int(currentChar) >= len(extendedChars) {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
										index}
								}
								val := extendedChars[currentChar]
								if val == 0 { //note that we already check for the currentChar '0' character at another else/if block, so any other character mapped to the value 0 is an invalid character
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
										index}
								} else if extendedCharacterIndex < 0 {
									extendedCharacterIndex = index
								}
							} else {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
									index}
							}
							index++
						}
					}
					continue
				}
			}
			// ipv6 and mac segments handled here
			segCount := parseData.getSegmentCount()
			var hexMaxChars int
			if isMac {
				if segCount == 0 {
					if isSingleSegment {
						parseData.initSegmentData(1)
					} else {
						if hexDelimiterIndex >= 0 {
							return &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
								hexDelimiterIndex}
						} else {
							var isNoExc bool
							if isRangeChar {
								isNoExc = macOptions.AllowsDashed()
							} else if isSpace {
								isNoExc = macOptions.AllowsSpaceDelimited()
							} else {
								isNoExc = macOptions.AllowsColonDelimited()
							}
							if !isNoExc {
								return &addressStringError{addressError{str: str, key: "ipaddress.mac.error.format"}}
							} else if isRangeChar {
								macFormat = dashed
								macParseData.setFormat(macFormat)
								checkCharCounts = false //counting chars later
							} else {
								if isSpace {
									macFormat = spaceDelimited
								} else {
									macFormat = colonDelimited
								}
								macParseData.setFormat(macFormat)
							}
						}
						parseData.initSegmentData(ExtendedUniqueIdentifier64SegmentCount)
						isSegmented = true
					}
				} else {
					isExc := false
					if isRangeChar {
						isExc = macFormat != dashed
					} else if isSpace {
						isExc = macFormat != spaceDelimited
					} else {
						isExc = macFormat != colonDelimited
					}
					if isExc {
						return &addressStringIndexError{
							addressStringError{addressError{str: str, key: "ipaddress.mac.error.mix.format.characters.at.index"}},
							index}
					}
					var segLimit int
					if macOptions.GetPreferredLen() == addrstrparam.MAC48Len {
						segLimit = MediaAccessControlSegmentCount
					} else {
						segLimit = ExtendedUniqueIdentifier64SegmentCount
					}
					if segCount >= segLimit {
						return &addressStringError{addressError{str: str, key: "ipaddress.error.too.many.segments"}}
					}
				}
				hexMaxChars = MACSegmentMaxChars //will be ignored for single or double segments due to checkCharCounts booleans
			} else {
				if segCount == 0 {
					if !validationOptions.AllowsIPv6() {
						return &addressStringError{addressError{str: str, key: "ipaddress.error.ipv6"}}
					}
					canBeBase85 = false
					version = IPv6
					ipParseData.setVersion(version)
					stringFormatParams = ipv6SpecificOptions
					isSegmented = true

					if index == strStartIndex {
						firstIndex := index
						index++
						if index == strEndIndex {
							return &addressStringError{addressError{str: str, key: "ipaddress.error.too.few.segments"}}
						} else if str[index] != IPv6SegmentSeparator {
							return &addressStringError{addressError{str: str, key: "ipaddress.error.ipv6.cannot.start.with.single.separator"}}
						}
						parseData.initSegmentData(IPv6SegmentCount)
						parseData.setConsecutiveSeparatorSegmentIndex(0)
						parseData.setConsecutiveSeparatorIndex(firstIndex)
						assign3Attributes(index, index, parseData, 0, index)
						parseData.incrementSegmentCount()
						index++
						segmentValueStartIndex = index
						segmentStartIndex = segmentValueStartIndex
						continue
					} else {
						if isSingleSegment {
							parseData.initSegmentData(1)
						} else {
							if hexDelimiterIndex >= 0 {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
									hexDelimiterIndex}
							}
							parseData.initSegmentData(IPv6SegmentCount)
						}
					}
				} else if ipParseData.getProviderIPVersion().IsIPv4() {
					return &addressStringError{addressError{str: str, key: "ipaddress.error.ipv6.separator"}}
				} else if segCount >= IPv6SegmentCount {
					return &addressStringError{addressError{str: str, key: "ipaddress.error.too.many.segments"}}
				}
				hexMaxChars = IPv6SegmentMaxChars // will be ignored for single segment due to checkCharCounts boolean
			}
			if index == segmentStartIndex { // empty segment
				if isMac {
					return &addressStringIndexError{
						addressStringError{addressError{str: str, key: "ipaddress.error.empty.segment.at.index"}},
						index}
				} else if parseData.getConsecutiveSeparatorIndex() >= 0 {
					return &addressStringError{addressError{str: str, key: "ipaddress.error.ipv6.ambiguous"}}
				}
				parseData.setConsecutiveSeparatorSegmentIndex(segCount)
				parseData.setConsecutiveSeparatorIndex(index - 1)
				assign3Attributes(index, index, parseData, segCount, index)
				parseData.incrementSegmentCount()
			} else if wildcardCount > 0 && !isSingleIPv6 {
				if !stringFormatParams.GetRangeParams().AllowsWildcard() {
					return &addressStringError{addressError{str: str, key: "ipaddress.error.no.wildcard"}}
				}
				totalDigits := index - segmentStartIndex
				if wildcardCount != totalDigits || hexDelimiterIndex >= 0 {
					return &addressStringIndexError{
						addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
						index}
				}
				parseData.setHasWildcard()
				startIndex := index - wildcardCount
				var maxVal uint64
				if isMac {
					if isDoubleSegment {
						maxVal = macMaxTriple
					} else {
						maxVal = MACMaxValuePerSegment
					}
				} else {
					maxVal = IPv6MaxValuePerSegment
				}
				assign6Attributes2Values1Flags(startIndex, index, startIndex, startIndex, index, startIndex,
					parseData, segCount, 0, maxVal, keyWildcard)
				parseData.incrementSegmentCount()
				wildcardCount = 0
			} else {
				startIndex := segmentValueStartIndex
				digitCount := index - startIndex
				noValuesToSet := false
				var value uint64
				var flags, rangeFlags uint32
				if leadingWithZero {
					if digitCount == 1 {
						// can only be a single 0
						if leadingZeroCount == 0 && rangeWildcardIndex < 0 {
							// handles 0 but not 1-0
							assign3Attributes(startIndex, index, parseData, segCount, segmentValueStartIndex)
							parseData.incrementSegmentCount()
							index++
							segmentValueStartIndex = index
							segmentStartIndex = segmentValueStartIndex
							leadingWithZero = false
							continue
						}
					} else {
						if hasDigits {
							leadingZeroCount++
						}
						startIndex += leadingZeroCount
						digitCount -= leadingZeroCount
					}
					leadingWithZero = false
				}
				if leadingZeroCount == 0 {
					if digitCount == 0 {
						// since we have already checked for an empty segment, this can only happen with a range, ie rangeWildcardIndex >= 0
						// we allow an empty range boundary to denote the max value
						if !stringFormatParams.GetRangeParams().AllowsInferredBoundary() {
							// starts with '.', or has two consecutive '.'
							return &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.empty.segment.at.index"}},
								index}
						} else if isMac {
							if isSingleSegment {
								if macParseData.isExtended() {
									value = 0xffffffffffffffff
								} else {
									value = 0xffffffffffff
								}
							} else {
								value = MACMaxValuePerSegment
							}
						} else {
							if isSingleIPv6 {
								value = 0xffffffffffffffff
								extendedValue = value
							} else {
								value = IPv6MaxValuePerSegment
							}
						}
						rangeFlags = keyInferredUpperBoundary
					} else {
						if digitCount > hexMaxChars && checkCharCounts {
							return &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
								segmentValueStartIndex}
						}
						if singleWildcardCount > 0 {
							noValuesToSet = true
							if rangeWildcardIndex >= 0 {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
									index}
							} else if isSingleIPv6 { //We need this special call here because single ipv6 hex is 128 bits and cannot fit into a long
								if parseErr := parseSingleSegmentSingleWildcard16(currentValueHex, str, startIndex, index, singleWildcardCount, parseData, segCount, segmentValueStartIndex, stringFormatParams); parseErr != nil {
									return parseErr
								}
							} else {
								if parseErr := assignSingleWildcard16(currentValueHex, str, startIndex, index, singleWildcardCount, parseData, segCount, segmentValueStartIndex, stringFormatParams); parseErr != nil {
									return parseErr
								}
							}
							uppercase = false
							singleWildcardCount = 0
						} else {
							if isSingleIPv6 { //We need this special branch here because single ipv6 hex is 128 bits and cannot fit into a long
								midIndex := index - 16
								if startIndex < midIndex {
									extendedValue = parseLong16(str, startIndex, midIndex)
									value = parseLong16(str, midIndex, index)
								} else {
									value = currentValueHex
								}
							} else {
								value = currentValueHex
								if uppercase {
									uppercase = false
								} else {
									flags = keyStandardStr
								}
							}
						}
						hasDigits = false
						currentValueHex = 0
					}
				} else {
					if leadingZeroCount == 1 && (digitCount == 17 || digitCount == 129) &&
						ipv6SpecificOptions.AllowsBinary() && isBinaryDelimiter(str, startIndex) {
						// IPv6 binary - to avoid ambiguity, all binary digits must be present, and preceded by 0b.
						// So for a single segment IPv6 segment:
						// 0b11 is a hex segment, 0b111 is hex, 0b1111 is invalid (too short for binary, too long for hex), 0b1100110011001100 is a binary segment
						if !stringFormatParams.AllowsLeadingZeros() {
							return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
						}
						startIndex++ // exclude the 'b' in 0b1100
						digitCount-- // exclude the 'b'
						if singleWildcardCount > 0 {
							if rangeWildcardIndex >= 0 {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
									index}
							} else if isSingleIPv6 {
								if parseErr := parseSingleSegmentSingleWildcard2(str, startIndex, index, singleWildcardCount, parseData, segCount, segmentValueStartIndex, stringFormatParams); parseErr != nil {
									return parseErr
								}
							} else {
								if parseErr := switchSingleWildcard2(currentValueHex, str, startIndex, index, singleWildcardCount, parseData, segCount, segmentValueStartIndex, stringFormatParams); parseErr != nil {
									return parseErr
								}
							}
							noValuesToSet = true
							singleWildcardCount = 0
						} else {
							if isSingleIPv6 { //We need this special branch here because single ipv6 hex is 128 bits and cannot fit into a long
								midIndex := index - 64
								extendedValue = parseLong2(str, startIndex, midIndex)
								value = parseLong2(str, midIndex, index)
							} else {
								value, err = switchValue2(currentValueHex, str, digitCount)
								if err != nil {
									return err
								}
							}
							flags = 2 // radix
						}
						ipParseData.setHasBinaryDigits(true)
						hasDigits = false
						currentValueHex = 0
					} else {
						if digitCount > hexMaxChars && checkCharCounts {
							return &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
								segmentValueStartIndex}
						} else if !stringFormatParams.AllowsLeadingZeros() {
							return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
						} else if !stringFormatParams.AllowsUnlimitedLeadingZeros() && checkCharCounts && (digitCount+leadingZeroCount) > hexMaxChars {
							return &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
								segmentValueStartIndex}
						}
						if singleWildcardCount > 0 {
							noValuesToSet = true
							if rangeWildcardIndex >= 0 {
								return &addressStringIndexError{
									addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
									index}
							} else if isSingleIPv6 { //We need this special call here because single ipv6 hex is 128 bits and cannot fit into a long
								if parseErr := parseSingleSegmentSingleWildcard16(currentValueHex, str, startIndex, index, singleWildcardCount, parseData, segCount, segmentValueStartIndex, stringFormatParams); parseErr != nil {
									return parseErr
								}
							} else {
								if parseErr := assignSingleWildcard16(currentValueHex, str, startIndex, index, singleWildcardCount, parseData, segCount, segmentValueStartIndex, stringFormatParams); parseErr != nil {
									return parseErr
								}
							}
							uppercase = false
							singleWildcardCount = 0
						} else {
							if isSingleIPv6 { //We need this special branch here because single ipv6 hex is 128 bits and cannot fit into a long
								midIndex := index - 16
								if startIndex < midIndex {
									extendedValue = parseLong16(str, startIndex, midIndex)
									value = parseLong16(str, midIndex, index)
								} else {
									value = currentValueHex
								}
							} else {
								value = currentValueHex
								if uppercase {
									uppercase = false
								} else {
									flags = keyStandardStr
								}
							}
						}
						hasDigits = false
						currentValueHex = 0
					}
				}
				if rangeWildcardIndex >= 0 {
					frontStartIndex := rangeWildcardIndex - frontDigitCount
					frontEndIndex := rangeWildcardIndex
					frontLeadingZeroStartIndex := frontStartIndex - frontLeadingZeroCount
					frontTotalDigitCount := frontDigitCount + frontLeadingZeroCount //the stuff that uses frontLeadingZeroCount needs to be sectioned off when singleIPv6
					if !stringFormatParams.GetRangeParams().AllowsRangeSeparator() {
						return &addressStringError{addressError{str: str, key: "ipaddress.error.no.range"}}
					} else if frontHexDelimiterIndex >= 0 && !isSingleSegment {
						return &addressStringIndexError{
							addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
							frontHexDelimiterIndex}
					} else if frontSingleWildcardCount > 0 || frontWildcardCount > 0 { // no wildcards in ranges
						return &addressStringIndexError{
							addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.combination.at.index"}},
							rangeWildcardIndex}
					} else if isMac && !macSpecificOptions.AllowsShortSegments() && frontTotalDigitCount < 2 {
						return &addressStringIndexError{
							addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.short.at.index"}},
							frontLeadingZeroStartIndex}
					}
					var upperRadix uint32
					frontIsBinary := false
					var front, extendedFront uint64
					frontEmpty := frontStartIndex == frontEndIndex
					isReversed := false
					if frontEmpty {
						if !stringFormatParams.GetRangeParams().AllowsInferredBoundary() {
							return &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.empty.segment.at.index"}},
								index}
						}
						rangeFlags |= keyInferredLowerBoundary
					} else {
						if frontLeadingZeroCount == 1 && frontDigitCount == 17 && ipv6SpecificOptions.AllowsBinary() && isBinaryDelimiter(str, frontStartIndex) {
							// IPv6 binary - to avoid ambiguity, all binary digits must be present, and preceded by 0b.
							frontStartIndex++ // exclude the 'b' in 0b1100
							frontDigitCount-- // exclude the 'b'
							front, err = switchValue2(currentFrontValueHex, str, frontDigitCount)
							if err != nil {
								return err
							}
							upperRadix = 2          // radix
							rangeFlags = upperRadix // radix
							ipParseData.setHasBinaryDigits(true)
							isReversed = front > value
							frontIsBinary = true
						} else if isSingleIPv6 { //We need this special block here because single ipv6 hex is 128 bits and cannot fit into a long
							if frontDigitCount == 129 { // binary
								frontStartIndex++ // exclude the 'b' in 0b1100
								frontDigitCount-- // exclude the 'b'
								upperRadix = 2    // radix
								rangeFlags = 2    // radix
								ipParseData.setHasBinaryDigits(true)
								frontMidIndex := frontEndIndex - 64
								extendedFront = parseLong2(str, frontStartIndex, frontMidIndex)
								front = parseLong2(str, frontMidIndex, frontEndIndex)
							} else {
								frontMidIndex := frontEndIndex - 16
								if frontStartIndex < frontMidIndex {
									extendedFront = parseLong16(str, frontStartIndex, frontMidIndex)
									front = parseLong16(str, frontMidIndex, frontEndIndex)
								} else {
									front = currentFrontValueHex
								}
							}
							isReversed = (extendedFront > extendedValue) || (extendedFront == extendedValue && front > value)
						} else {
							if !stringFormatParams.AllowsLeadingZeros() && frontLeadingZeroCount > 0 {
								return &addressStringError{addressError{str: str, key: "ipaddress.error.segment.leading.zeros"}}
							} else if checkCharCounts {
								if frontDigitCount > hexMaxChars {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
										frontLeadingZeroStartIndex}
								} else if !stringFormatParams.AllowsUnlimitedLeadingZeros() && frontTotalDigitCount > hexMaxChars {
									return &addressStringIndexError{
										addressStringError{addressError{str: str, key: "ipaddress.error.segment.too.long.at.index"}},
										frontLeadingZeroStartIndex}
								}
							}
							front = currentFrontValueHex
							isReversed = front > value
							extendedFront = 0
						}
					}
					backEndIndex := index
					if isReversed {
						if !stringFormatParams.GetRangeParams().AllowsReverseRange() {
							return &addressStringError{addressError{str: str, key: "ipaddress.error.invalidRange"}}
						}
						// switcheroo
						frontStartIndex, startIndex = startIndex, frontStartIndex
						frontEndIndex, backEndIndex = backEndIndex, frontEndIndex
						frontLeadingZeroStartIndex, segmentValueStartIndex = segmentValueStartIndex, frontLeadingZeroStartIndex
						front, value = value, front
						extendedFront, extendedValue = extendedValue, extendedFront
					}
					if isSingleIPv6 {
						assign7Attributes4Values1Flags(frontStartIndex, frontEndIndex, frontLeadingZeroStartIndex, startIndex, backEndIndex, segmentValueStartIndex,
							parseData, segCount, front, extendedFront, value, extendedValue, rangeFlags|keyRangeWildcard, upperRadix)
					} else {
						if !frontUppercase && !frontEmpty && !isReversed && !frontIsBinary {
							if leadingZeroCount == 0 && (flags&keyStandardStr) != 0 && frontIsStandardRangeChar {
								rangeFlags |= keyStandardRangeStr | keyStandardStr
							} else {
								rangeFlags |= keyStandardStr
							}
						}
						assign6Attributes2Values2Flags(frontStartIndex, frontEndIndex, frontLeadingZeroStartIndex, startIndex, backEndIndex, segmentValueStartIndex,
							parseData, segCount, front, value, rangeFlags|keyRangeWildcard, upperRadix)
					}
					rangeWildcardIndex = -1
				} else if !noValuesToSet {
					if isSingleIPv6 {
						assign3Attributes2Values1Flags(startIndex, index, segmentValueStartIndex, parseData, segCount, value, extendedValue, flags)
					} else {
						assign3Attributes1Values1Flags(startIndex, index, segmentValueStartIndex, parseData, segCount, value, flags)
					}
				}
				parseData.incrementSegmentCount()
				leadingZeroCount = 0
			}
			index++
			segmentValueStartIndex = index
			segmentStartIndex = segmentValueStartIndex
			// end of IPv6 and MAC48Len segments
		} // end of all cases
	} // end of character loop
	return nil
}

func (strValidator) validatePrefixLenStr(fullAddr string, version IPVersion) (prefixLen PrefixLen, err addrerr.AddressStringError) {
	var qualifier parsedHostIdentifierStringQualifier
	isPrefix, err := validatePrefix(fullAddr, nil, defaultIPAddrParameters, nil,
		&qualifier, 0, len(fullAddr), version)
	if !isPrefix {
		err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalidCIDRPrefix"}}
	} else {
		prefixLen = qualifier.getNetworkPrefixLen()
	}
	return
}

func parsePortOrService(
	fullAddr string,
	zone *Zone,
	validationOptions addrstrparam.HostNameParams,
	res *parsedHostIdentifierStringQualifier,
	index,
	endIndex int) (err addrerr.AddressStringError) {
	isPort := true
	var hasLetter, hasDigits, isAll bool
	var charCount, digitCount int
	var port int
	lastHyphen := -1
	charArray := chars
	for i := index; i < endIndex; i++ {
		c := fullAddr[i]
		if c >= '1' && c <= '9' {
			if isPort {
				digitCount++
				if digitCount > 5 { // 65535 is max
					isPort = false
				} else {
					hasDigits = true
					port = port*10 + int(charArray[c])
				}
			}
			charCount++
		} else if c == '0' {
			if isPort && hasDigits {
				digitCount++
				if digitCount > 5 { // 65535 is max
					isPort = false
				} else {
					port *= 10
				}
			}
			charCount++
		} else {
			//http://www.iana.org/assignments/port-numbers
			//valid service name chars:
			//https://tools.ietf.org/html/rfc6335#section-5.1
			//https://tools.ietf.org/html/rfc6335#section-10.1
			isPort = false
			isHyphen := c == '-'
			isAll = c == SegmentWildcard
			if (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || isHyphen || isAll {
				if isHyphen {
					if i == index {
						err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.host.error.invalid.service.hyphen.start"}}
						return
					} else if i-1 == lastHyphen {
						err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.host.error.invalid.service.hyphen.consecutive"}}
						return
					} else if i == endIndex-1 {
						err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.host.error.invalid.service.hyphen.end"}}
						return
					}
					lastHyphen = i
				} else if isAll {
					if i > index {
						err = &addressStringIndexError{
							addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.character.combination.at.index"}},
							i}
						return
					} else if i+1 < endIndex {
						err = &addressStringIndexError{
							addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.character.combination.at.index"}},
							i + 1}
						return
					}
					hasLetter = true
					charCount++
					break
				} else {
					hasLetter = true
				}
				charCount++
			} else {
				err = &addressStringIndexError{
					addressStringError{addressError{str: fullAddr, key: "ipaddress.host.error.invalid.port.service"}},
					i}
				return
			}
		}
	}
	if isPort {
		if !validationOptions.AllowsPort() {
			err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.host.error.port"}}
			return
		} else if port == 0 {
			err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.host.error.invalidPort.no.digits"}}
			return
		} else if port > 65535 {
			err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.host.error.invalidPort.too.large"}}
			return
		}
		res.setZone(zone)
		res.port = cachePorts(PortInt(port))
		return
	} else if !validationOptions.AllowsService() {
		err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.host.error.service"}}
		return
	} else if charCount == 0 {
		err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.host.error.invalidService.no.chars"}}
		return
	} else if charCount > 15 {
		err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.host.error.invalidService.too.long"}}
		return
	} else if !hasLetter {
		err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.host.error.invalidService.no.letter"}}
		return
	}
	res.setZone(zone)
	res.service = fullAddr[index:endIndex]
	return
}

func parseValidatedPrefix(
	result BitCount,
	fullAddr string,
	zone *Zone,
	validationOptions addrstrparam.IPAddressStringParams,
	res *parsedHostIdentifierStringQualifier,
	digitCount,
	leadingZeros int,
	ipVersion IPVersion) (err addrerr.AddressStringError) {
	if digitCount == 0 {
		//we know leadingZeroCount is > 0 since we have checked already if there were no characters at all
		leadingZeros--
		// digitCount++ digitCount is unused after this, no need for it to be accurate
	}
	asIPv4 := ipVersion.IsIPv4()
	if asIPv4 {
		if leadingZeros > 0 && !validationOptions.GetIPv4Params().AllowsPrefixLenLeadingZeros() {
			err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.ipv4.prefix.leading.zeros"}}
			return
		}
		if result > IPv4BitCount {
			allowPrefixesBeyondAddressSize := validationOptions.GetIPv4Params().AllowsPrefixesBeyondAddressSize()
			if !allowPrefixesBeyondAddressSize {
				err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.prefixSize"}}
				return
			}
			result = IPv4BitCount
		}
	} else {
		if leadingZeros > 0 && !validationOptions.GetIPv6Params().AllowsPrefixLenLeadingZeros() {
			err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.ipv6.prefix.leading.zeros"}}
			return
		}
		if result > IPv6BitCount {
			allowPrefixesBeyondAddressSize := validationOptions.GetIPv6Params().AllowsPrefixesBeyondAddressSize()
			if !allowPrefixesBeyondAddressSize {
				err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.prefixSize"}}
				return
			}
			result = IPv6BitCount
		}
	}
	res.networkPrefixLength = cacheBitCount(result)
	res.setZone(zone)
	return
}

func validatePrefix(
	fullAddr string,
	zone *Zone,
	validationOptions addrstrparam.IPAddressStringParams,
	hostValidationOptions addrstrparam.HostNameParams,
	res *parsedHostIdentifierStringQualifier,
	index,
	endIndex int,
	ipVersion IPVersion) (isPrefix bool, err addrerr.AddressStringError) {
	if index == len(fullAddr) {
		return
	}
	isPrefix = true
	prefixEndIndex := endIndex
	hasDigits := false
	var result BitCount
	var leadingZeros int
	charArray := chars
	for i := index; i < endIndex; i++ {
		c := fullAddr[i]
		if c >= '1' && c <= '9' {
			hasDigits = true
			result = result*10 + BitCount(charArray[c])
		} else if c == '0' {
			if hasDigits {
				result *= 10
			} else {
				leadingZeros++
			}
		} else if c == PortSeparator && hostValidationOptions != nil &&
			(hostValidationOptions.AllowsPort() || hostValidationOptions.AllowsService()) && i > index {
			// check if we have a port or service.  If not, possibly an IPv6 mask.
			// Also, parsing for port first (rather than prefix) allows us to call
			// parseValidatedPrefix with the knowledge that whatever is supplied can only be a prefix.
			portErr := parsePortOrService(fullAddr, zone, hostValidationOptions, res, i+1, endIndex)
			//portQualifier, err = parsePortOrService(fullAddr, zone, hostValidationOptions, res, i+1, endIndex)
			if portErr != nil {
				return
			}
			prefixEndIndex = i
			break
		} else {
			isPrefix = false
			break
		}
	}
	//we treat as a prefix if all the characters were digits, even if there were too many, unless the mask options allow for inet_aton single segment
	if isPrefix {
		err = parseValidatedPrefix(result, fullAddr,
			zone, validationOptions, res, prefixEndIndex-index /* digitCount */, leadingZeros, ipVersion)
	}
	return
}

func parseAddressQualifier(
	fullAddr string,
	validationOptions addrstrparam.IPAddressStringParams,
	hostValidationOptions addrstrparam.HostNameParams,
	ipAddressParseData *ipAddressParseData,
	endIndex int) (err addrerr.AddressStringError) {
	qualifierIndex := ipAddressParseData.getQualifierIndex()
	addressIsEmpty := ipAddressParseData.getAddressParseData().isProvidingEmpty()
	ipVersion := ipAddressParseData.getProviderIPVersion()
	res := ipAddressParseData.getQualifier()
	if ipAddressParseData.hasPrefixSeparator() {
		return parsePrefix(fullAddr, nil, validationOptions, hostValidationOptions,
			res, addressIsEmpty, qualifierIndex, endIndex, ipVersion)
	} else if ipAddressParseData.isZoned() {
		if ipAddressParseData.isBase85Zoned() && !ipAddressParseData.isProvidingBase85IPv6() {
			err = &addressStringIndexError{
				addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.character.at.index"}},
				qualifierIndex - 1}
			return
		}
		if addressIsEmpty {
			err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.only.zone"}}
			return
		}
		return parseZone(fullAddr, validationOptions, res, addressIsEmpty, qualifierIndex, endIndex, ipVersion)
	}
	return
}

func parseHostAddressQualifier(
	fullAddr string,
	validationOptions addrstrparam.IPAddressStringParams,
	hostValidationOptions addrstrparam.HostNameParams,
	isPrefixed,
	hasPort bool,
	ipAddressParseData *ipAddressParseData,
	qualifierIndex,
	endIndex int) (err addrerr.AddressStringError) {
	res := ipAddressParseData.getQualifier()
	addressIsEmpty := ipAddressParseData.getAddressParseData().isProvidingEmpty()
	ipVersion := ipAddressParseData.getProviderIPVersion()
	if isPrefixed {
		return parsePrefix(fullAddr, nil, validationOptions, hostValidationOptions,
			res, addressIsEmpty, qualifierIndex, endIndex, ipVersion)
	} else if ipAddressParseData.isZoned() {
		if addressIsEmpty {
			err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.only.zone"}}
			return
		}
		return parseEncodedZone(fullAddr, validationOptions, res, addressIsEmpty, qualifierIndex, endIndex, ipVersion)
	} else if hasPort { //isPort is always false when validating an address
		return parsePortOrService(fullAddr, nil, hostValidationOptions, res, qualifierIndex, endIndex)
	}
	return
}

func parsePrefix(
	fullAddr string,
	zone *Zone,
	validationOptions addrstrparam.IPAddressStringParams,
	hostValidationOptions addrstrparam.HostNameParams,
	res *parsedHostIdentifierStringQualifier,
	addressIsEmpty bool,
	index,
	endIndex int,
	ipVersion IPVersion) (err addrerr.AddressStringError) {
	if validationOptions.AllowsPrefix() {
		var isPrefix bool
		isPrefix, err = validatePrefix(fullAddr, zone, validationOptions, hostValidationOptions,
			res, index, endIndex, ipVersion)
		if err != nil || isPrefix {
			return
		}
	}
	if addressIsEmpty {
		err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.mask.address.empty"}}
	} else if validationOptions.AllowsMask() {
		//check for a mask
		//check if we need a new validation options for the mask
		maskOptions := toMaskOptions(validationOptions, ipVersion)
		pa := &parsedIPAddress{
			ipAddressParseData: ipAddressParseData{addressParseData: addressParseData{str: fullAddr}},
			options:            maskOptions,
		}
		err = validateIPAddress(maskOptions, fullAddr, index, endIndex, pa.getIPAddressParseData(), false)
		if err != nil {
			err = &addressStringNestedError{
				addressStringError: addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalidCIDRPrefixOrMask"}},
				nested:             err,
			}
			return
		}
		maskParseData := pa.getAddressParseData()
		if maskParseData.isProvidingEmpty() {
			err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.mask.empty"}}
			return
		} else if maskParseData.isAll() {
			err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.mask.wildcard"}}
			return
		}
		err = checkSegments(fullAddr, maskOptions, pa.getIPAddressParseData())
		if err != nil {
			err = &addressStringNestedError{
				addressStringError: addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalidCIDRPrefixOrMask"}},
				nested:             err,
			}
			return
		}
		maskEndIndex := maskParseData.getAddressEndIndex()
		if maskEndIndex != endIndex { // 1.2.3.4/ or 1.2.3.4// or 1.2.3.4/%
			err = &addressStringIndexError{
				addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.mask.extra.chars"}},
				maskEndIndex + 1}
			return
		}
		maskVersion := pa.getProviderIPVersion()
		if maskVersion.IsIPv4() && maskParseData.getSegmentCount() == 1 && !maskParseData.hasWildcard() &&
			!validationOptions.GetIPv4Params().Allows_inet_aton_single_segment_mask() { //1.2.3.4/33 where 33 is an aton_inet single segment address and not a prefix length
			err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.mask.single.segment"}}
			return
		} else if !ipVersion.IsIndeterminate() && (maskVersion.IsIPv4() != ipVersion.IsIPv4() || maskVersion.IsIPv6() != ipVersion.IsIPv6()) {
			//note that this also covers the cases of non-standard addresses in the mask, ie mask neither ipv4 or ipv6
			err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.ipMismatch"}}
			return
		}
		res.mask = pa
		res.setZone(zone)
	} else if validationOptions.AllowsPrefix() {
		err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalidCIDRPrefixOrMask"}}
	} else {
		err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.CIDRNotAllowed"}}
	}
	return
}

func parseHostNameQualifier(
	fullAddr string,
	validationOptions addrstrparam.IPAddressStringParams,
	hostValidationOptions addrstrparam.HostNameParams,
	res *parsedHostIdentifierStringQualifier,
	isPrefixed,
	isPort, // always false for address
	addressIsEmpty bool,
	index,
	endIndex int,
	ipVersion IPVersion) (err addrerr.AddressStringError) {
	if isPrefixed {
		return parsePrefix(fullAddr, nil, validationOptions, hostValidationOptions,
			res, addressIsEmpty, index, endIndex, ipVersion)
	} else if isPort { // isPort is always false when validating an address
		return parsePortOrService(fullAddr, nil, hostValidationOptions, res, index, endIndex)
	}
	//res = noQualifier
	return
}

// ValidateZoneStr returns an error if the given zone is invalid
func ValidateZoneStr(zoneStr string) (zone Zone, err addrerr.AddressStringError) {
	for i := 0; i < len(zoneStr); i++ {
		c := zone[i]
		if c == PrefixLenSeparator {
			err = &addressStringIndexError{addressStringError{addressError{str: zoneStr, key: "ipaddress.error.invalid.zone"}}, i}
			return
		}
		if c == IPv6SegmentSeparator {
			err = &addressStringIndexError{addressStringError{addressError{str: zoneStr, key: "ipaddress.error.invalid.zone"}}, i}
			return
		}
	}
	return Zone(zoneStr), nil
}

func isReserved(c byte) bool {
	isUnreserved :=
		(c >= '0' && c <= '9') ||
			(c >= 'A' && c <= 'Z') ||
			(c >= 'a' && c <= 'z') ||
			c == RangeSeparator ||
			c == LabelSeparator ||
			c == '_' ||
			c == '~'
	return !isUnreserved
}

func parseZone(
	fullAddr string,
	validationOptions addrstrparam.IPAddressStringParams,
	res *parsedHostIdentifierStringQualifier,
	addressIsEmpty bool,
	index,
	endIndex int,
	ipVersion IPVersion) (err addrerr.AddressStringError) {
	if index == endIndex && !validationOptions.GetIPv6Params().AllowsEmptyZone() {
		err = &addressStringIndexError{addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.zone"}}, index}
		return
	}
	for i := index; i < endIndex; i++ {
		c := fullAddr[i]
		if c == PrefixLenSeparator {
			if i == index && !validationOptions.GetIPv6Params().AllowsEmptyZone() {
				err = &addressStringIndexError{addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.zone"}}, index}
				return
			}
			zone := Zone(fullAddr[index:i])
			return parsePrefix(fullAddr, &zone, validationOptions, nil, res, addressIsEmpty, i+1, endIndex, ipVersion)
		} else if c == IPv6SegmentSeparator {
			err = &addressStringIndexError{
				addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.zone"}},
				i}
			return
		}
	}
	z := Zone(fullAddr[index:endIndex])
	res.setZone(&z)
	return
}

func parseEncodedZone(
	fullAddr string,
	validationOptions addrstrparam.IPAddressStringParams,
	res *parsedHostIdentifierStringQualifier,
	addressIsEmpty bool,
	index,
	endIndex int,
	ipVersion IPVersion) (err addrerr.AddressStringError) {
	if index == endIndex && !validationOptions.GetIPv6Params().AllowsEmptyZone() {
		err = &addressStringIndexError{addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.zone"}}, index}
		return
	}
	var result strings.Builder
	var zone string
	for i := index; i < endIndex; i++ {
		c := fullAddr[i]

		//we are in here when we have a square bracketed host like [::1]
		//not if we have a HostName with no brackets

		//https://tools.ietf.org/html/rfc6874
		//https://tools.ietf.org/html/rfc4007#section-11.7
		if c == IPv6ZoneSeparator {
			if i+2 >= endIndex {
				err = &addressStringIndexError{
					addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.zone.encoding"}},
					i}
				return
			}
			//percent encoded
			if result.Cap() == 0 {
				result.Grow(endIndex - index)
				result.WriteString(fullAddr[index:i])
			}
			charArray := chars
			i++
			c = charArray[fullAddr[i]] << 4
			i++
			c |= charArray[fullAddr[i]]
		} else if c == PrefixLenSeparator {
			if i == index && !validationOptions.GetIPv6Params().AllowsEmptyZone() {
				err = &addressStringIndexError{addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.zone"}}, index}
				return
			}
			if result.Cap() > 0 {
				zone = result.String()
			} else {
				zone = fullAddr[index:i]
			}
			z := Zone(zone)
			return parsePrefix(fullAddr, &z, validationOptions, nil, res, addressIsEmpty, i+1, endIndex, ipVersion)
		} else if isReserved(c) {
			err = &addressStringIndexError{
				addressStringError{addressError{str: fullAddr, key: "ipaddress.error.invalid.zone"}},
				i}
			return
		}
		if result.Cap() > 0 {
			result.WriteByte(c)
		}
	}
	if result.Len() == 0 {
		zone = fullAddr[index:endIndex]
	} else {
		zone = result.String()
	}
	z := Zone(zone)
	res.setZone(&z)
	return
}

// whether no wildcards or range characters allowed
func isNoRange(rp addrstrparam.RangeParams) bool {
	return !rp.AllowsWildcard() && !rp.AllowsRangeSeparator() && !rp.AllowsSingleWildcard()
}

/**
 * Some options are not supported in masks (prefix, wildcards, etc)
 * So we eliminate those options while preserving the others from the address options.
 * @param validationOptions
 * @param ipVersion
 * @return
 */
func toMaskOptions(validationOptions addrstrparam.IPAddressStringParams,
	ipVersion IPVersion) (res addrstrparam.IPAddressStringParams) {
	//We must provide options that do not allow a mask with wildcards or ranges
	var builder *addrstrparam.IPAddressStringParamsBuilder
	if ipVersion.IsIndeterminate() || ipVersion.IsIPv6() {
		ipv6Options := validationOptions.GetIPv6Params()
		if !isNoRange(ipv6Options.GetRangeParams()) {
			builder = new(addrstrparam.IPAddressStringParamsBuilder).Set(validationOptions)
			builder.GetIPv6AddressParamsBuilder().SetRangeParams(addrstrparam.NoRange)
		}
		if ipv6Options.AllowsMixed() && !isNoRange(ipv6Options.GetMixedParams().GetIPv4Params().GetRangeParams()) {
			if builder == nil {
				builder = new(addrstrparam.IPAddressStringParamsBuilder).Set(validationOptions)
			}
			builder.GetIPv6AddressParamsBuilder().SetRangeParams(addrstrparam.NoRange)
		}
	}
	if ipVersion.IsIndeterminate() || ipVersion.IsIPv4() {
		ipv4Options := validationOptions.GetIPv4Params()
		if !isNoRange(ipv4Options.GetRangeParams()) {
			if builder == nil {
				builder = new(addrstrparam.IPAddressStringParamsBuilder).Set(validationOptions)
			}
			builder.GetIPv4AddressParamsBuilder().SetRangeParams(addrstrparam.NoRange)
		}
	}
	if validationOptions.AllowsAll() {
		if builder == nil {
			builder = new(addrstrparam.IPAddressStringParamsBuilder).Set(validationOptions)
		}
		builder.AllowAll(false)
	}
	if builder == nil {
		res = validationOptions
	} else {
		res = builder.ToParams()
	}
	return
}

func assign3Attributes(start, end int, parseData *addressParseData, parsedSegIndex, leadingZeroStartIndex int) {
	ustart := uint32(start)
	uend := uint32(end)
	uleadingZeroStart := uint32(leadingZeroStartIndex)
	parseData.setIndex(parsedSegIndex,
		keyLowerStrDigitsIndex, uleadingZeroStart,
		keyLowerStrStartIndex, ustart,
		keyLowerStrEndIndex, uend,
		keyUpperStrDigitsIndex, uleadingZeroStart,
		keyUpperStrStartIndex, ustart,
		keyUpperStrEndIndex, uend)
}

func assign4Attributes(start, end int, parseData *addressParseData, parsedSegIndex, radix, leadingZeroStartIndex int) {
	ustart := uint32(start)
	uend := uint32(end)
	uleadingZeroStart := uint32(leadingZeroStartIndex)
	parseData.set7IndexFlags(parsedSegIndex,
		keyLowerRadixIndex, uint32(radix),
		keyLowerStrDigitsIndex, uleadingZeroStart,
		keyLowerStrStartIndex, ustart,
		keyLowerStrEndIndex, uend,
		keyUpperStrDigitsIndex, uleadingZeroStart,
		keyUpperStrStartIndex, ustart,
		keyUpperStrEndIndex, uend)
}

func assign7Attributes4Values1Flags(frontStart, frontEnd, frontLeadingZeroStartIndex, start, end, leadingZeroStartIndex int,
	parseData *addressParseData, parsedSegIndex int, frontValue, frontExtendedValue, value, extendedValue uint64, flags uint32, upperRadix uint32) {
	parseData.set8Index4ValuesFlags(parsedSegIndex,
		flagsIndex, flags,
		keyLowerStrDigitsIndex, uint32(frontLeadingZeroStartIndex),
		keyLowerStrStartIndex, uint32(frontStart),
		keyLowerStrEndIndex, uint32(frontEnd),
		keyUpperRadixIndex, uint32(upperRadix),
		keyUpperStrDigitsIndex, uint32(leadingZeroStartIndex),
		keyUpperStrStartIndex, uint32(start),
		keyUpperStrEndIndex, uint32(end),
		keyLower, frontValue,
		keyExtendedLower, frontExtendedValue,
		keyUpper, value,
		keyExtendedUpper, extendedValue)
}

func assign6Attributes4Values1Flags(frontStart, frontEnd, frontLeadingZeroStartIndex, start, end, leadingZeroStartIndex int,
	parseData *addressParseData, parsedSegIndex int, frontValue, frontExtendedValue, value, extendedValue uint64, flags uint32) {
	parseData.set7Index4ValuesFlags(parsedSegIndex,
		flagsIndex, flags,
		keyLowerStrDigitsIndex, uint32(frontLeadingZeroStartIndex),
		keyLowerStrStartIndex, uint32(frontStart),
		keyLowerStrEndIndex, uint32(frontEnd),
		keyUpperStrDigitsIndex, uint32(leadingZeroStartIndex),
		keyUpperStrStartIndex, uint32(start),
		keyUpperStrEndIndex, uint32(end),
		keyLower, frontValue,
		keyExtendedLower, frontExtendedValue,
		keyUpper, value,
		keyExtendedUpper, extendedValue)
}

func assign6Attributes2Values1Flags(frontStart, frontEnd, frontLeadingZeroStartIndex, start, end, leadingZeroStartIndex int,
	parseData *addressParseData, parsedSegIndex int, frontValue, value uint64, flags uint32) {
	parseData.set7Index2ValuesFlags(parsedSegIndex,
		flagsIndex, flags,
		keyLowerStrDigitsIndex, uint32(frontLeadingZeroStartIndex),
		keyLowerStrStartIndex, uint32(frontStart),
		keyLowerStrEndIndex, uint32(frontEnd),
		keyUpperStrDigitsIndex, uint32(leadingZeroStartIndex),
		keyUpperStrStartIndex, uint32(start),
		keyUpperStrEndIndex, uint32(end),
		keyLower, frontValue,
		keyUpper, value)
}

func assign6Attributes2Values2Flags(frontStart, frontEnd, frontLeadingZeroStartIndex, start, end, leadingZeroStartIndex int,
	parseData *addressParseData, parsedSegIndex int, frontValue, value uint64, flags /* includes lower radix */ uint32, upperRadix uint32) {
	parseData.set8Index2ValuesFlags(parsedSegIndex,
		flagsIndex, flags,
		keyLowerStrDigitsIndex, uint32(frontLeadingZeroStartIndex),
		keyLowerStrStartIndex, uint32(frontStart),
		keyLowerStrEndIndex, uint32(frontEnd),
		keyUpperRadixIndex, uint32(upperRadix),
		keyUpperStrDigitsIndex, uint32(leadingZeroStartIndex),
		keyUpperStrStartIndex, uint32(start),
		keyUpperStrEndIndex, uint32(end),
		keyLower, frontValue,
		keyUpper, value)
}

func assign3Attributes2Values1Flags(start, end, leadingZeroStart int,
	parseData *addressParseData, parsedSegIndex int, value, extendedValue uint64, flags uint32) {
	ustart := uint32(start)
	uend := uint32(end)
	uleadingZeroStart := uint32(leadingZeroStart)
	parseData.set7Index4ValuesFlags(parsedSegIndex,
		flagsIndex, flags,
		keyLowerStrDigitsIndex, uleadingZeroStart,
		keyLowerStrStartIndex, ustart,
		keyLowerStrEndIndex, uend,
		keyUpperStrDigitsIndex, uleadingZeroStart,
		keyUpperStrStartIndex, ustart,
		keyUpperStrEndIndex, uend,
		keyLower, value,
		keyExtendedLower, extendedValue,
		keyUpper, value,
		keyExtendedUpper, extendedValue)
}

func assign3Attributes1Values1Flags(start, end, leadingZeroStart int,
	parseData *addressParseData, parsedSegIndex int, value uint64, flags uint32) {
	ustart := uint32(start)
	uend := uint32(end)
	uleadingZeroStart := uint32(leadingZeroStart)
	parseData.set7Index2ValuesFlags(parsedSegIndex,
		flagsIndex, flags,
		keyUpperStrDigitsIndex, uleadingZeroStart,
		keyLowerStrDigitsIndex, uleadingZeroStart,
		keyUpperStrStartIndex, ustart,
		keyLowerStrStartIndex, ustart,
		keyUpperStrEndIndex, uend,
		keyLowerStrEndIndex, uend,
		keyLower, value,
		keyUpper, value)
}

func isBinaryDelimiter(str string, index int) bool {
	c := str[index]
	return c == 'b' || c == 'B'
}

func isHexDelimiter(c byte) bool {
	return c == 'x' || c == 'X'
}

var lowBitsVal uint64 = 0xffffffffffffffff
var lowBitsMask = new(big.Int).SetUint64(lowBitsVal)

func parseBase85(
	validationOptions addrstrparam.IPAddressStringParams,
	str string,
	strStartIndex,
	strEndIndex int,
	ipAddressParseData *ipAddressParseData,
	extendedRangeWildcardIndex,
	totalCharacterCount,
	index int) (bool, addrerr.AddressStringError) {

	parseData := ipAddressParseData.getAddressParseData()
	if extendedRangeWildcardIndex < 0 {
		if totalCharacterCount == ipv6Base85SingleSegmentDigitCount {
			if !validationOptions.AllowsIPv6() {
				return false, &addressStringError{addressError{str: str, key: "ipaddress.error.ipv6"}}
			}
			ipAddressParseData.setVersion(IPv6)
			val := parse85(str, strStartIndex, strEndIndex)
			var lowBits, highBits big.Int
			lowBits.And(val, lowBitsMask)
			val.Rsh(val, 64)
			highBits.And(val, lowBitsMask)
			val.Rsh(val, 64)
			//note that even with the correct number of base-85 digits, we can have a value too large
			if !bigIsZero(val) {
				return false, &addressStringError{addressError{str: str, key: "ipaddress.error.address.too.large"}}
			}
			value, extendedValue := lowBits.Uint64(), highBits.Uint64()
			parseData.initSegmentData(1)
			parseData.incrementSegmentCount()
			assign3Attributes2Values1Flags(strStartIndex, strEndIndex, strStartIndex, parseData, 0, value, extendedValue, 85)
			ipAddressParseData.setBase85(true)
			return true, nil
		}
	} else {
		if totalCharacterCount == (ipv6Base85SingleSegmentDigitCount<<1)+len(IPv6AlternativeRangeSeparatorStr) /* two base 85 addresses */ ||
			(totalCharacterCount == ipv6Base85SingleSegmentDigitCount+len(IPv6AlternativeRangeSeparatorStr) &&
				(extendedRangeWildcardIndex == 0 || extendedRangeWildcardIndex+len(IPv6AlternativeRangeSeparatorStr) == strEndIndex)) { /* note that we already check that extendedRangeWildcardIndex is at index 20 */
			if !validationOptions.AllowsIPv6() {
				return false, &addressStringError{addressError{str: str, key: "ipaddress.error.ipv6"}}
			}
			ipv6SpecificOptions := validationOptions.GetIPv6Params()
			if !ipv6SpecificOptions.GetRangeParams().AllowsRangeSeparator() {
				return false, &addressStringError{addressError{str: str, key: "ipaddress.error.no.range"}}
			}
			ipAddressParseData.setVersion(IPv6)
			frontEndIndex, flags := extendedRangeWildcardIndex, uint32(0)
			var value, value2, extendedValue, extendedValue2 uint64
			var lowerStart, lowerEnd, upperStart, upperEnd int
			//
			if frontEndIndex == strStartIndex+ipv6Base85SingleSegmentDigitCount {
				val := parse85(str, strStartIndex, frontEndIndex)
				var lowBits, highBits big.Int
				lowBits.And(val, lowBitsMask)
				val.Rsh(val, 64)
				highBits.And(val, lowBitsMask)
				value, extendedValue = lowBits.Uint64(), highBits.Uint64()
				if frontEndIndex+len(IPv6AlternativeRangeSeparatorStr) < strEndIndex {
					val2 := parse85(str, frontEndIndex+len(IPv6AlternativeRangeSeparatorStr), strEndIndex)
					var lowBits, highBits big.Int
					lowBits.And(val2, lowBitsMask)
					val2.Rsh(val2, 64)
					highBits.And(val2, lowBitsMask)
					value2, extendedValue2 = lowBits.Uint64(), highBits.Uint64()
					if val.Cmp(val2) > 0 {
						if !ipv6SpecificOptions.GetRangeParams().AllowsReverseRange() {
							return false, &addressStringError{addressError{str: str, key: "ipaddress.error.invalidRange"}}
						}
						//note that even with the correct number of base-85 digits, we can have a value too large
						val.Rsh(val, 64)
						if !bigIsZero(val) {
							return false, &addressStringError{addressError{str: str, key: "ipaddress.error.address.too.large"}}
						}
						lowerStart = frontEndIndex + len(IPv6AlternativeRangeSeparatorStr)
						lowerEnd = strEndIndex
						upperStart = strStartIndex
						upperEnd = frontEndIndex
					} else {
						//note that even with the correct number of base-85 digits, we can have a value too large
						val2.Rsh(val2, 64)
						if !bigIsZero(val2) {
							return false, &addressStringError{addressError{str: str, key: "ipaddress.error.address.too.large"}}
						}
						lowerStart = strStartIndex
						lowerEnd = frontEndIndex
						upperStart = frontEndIndex + len(IPv6AlternativeRangeSeparatorStr)
						upperEnd = strEndIndex
					}
				} else {
					if !ipv6SpecificOptions.GetRangeParams().AllowsInferredBoundary() {
						return false, &addressStringIndexError{
							addressStringError{addressError{str: str, key: "ipaddress.error.empty.segment.at.index"}},
							extendedRangeWildcardIndex}
					}
					lowerStart = strStartIndex
					lowerEnd = frontEndIndex
					upperStart = strEndIndex
					upperEnd = strEndIndex
					value2 = lowBitsVal
					extendedValue2 = lowBitsVal
					flags = keyInferredUpperBoundary
				}
			} else if frontEndIndex == 0 {
				if !ipv6SpecificOptions.GetRangeParams().AllowsInferredBoundary() {
					return false, &addressStringIndexError{
						addressStringError{addressError{str: str, key: "ipaddress.error.empty.segment.at.index"}},
						index}
				}
				flags = keyInferredLowerBoundary
				val2 := parse85(str, frontEndIndex+len(IPv6AlternativeRangeSeparatorStr), strEndIndex)
				var lowBits, highBits big.Int
				lowBits.And(val2, lowBitsMask)
				val2.Rsh(val2, 64)
				highBits.And(val2, lowBitsMask)
				val2.Rsh(val2, 64)
				//note that even with the correct number of base-85 digits, we can have a value too large
				if !bigIsZero(val2) {
					return false, &addressStringError{addressError{str: str, key: "ipaddress.error.address.too.large"}}
				}
				value2, extendedValue2 = lowBits.Uint64(), highBits.Uint64()
				upperStart = len(IPv6AlternativeRangeSeparatorStr)
				upperEnd = strEndIndex
			} else {
				return false, &addressStringIndexError{
					addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
					extendedRangeWildcardIndex}
			}
			parseData.incrementSegmentCount()
			parseData.initSegmentData(1)
			//parseData.setHasRange();
			assign7Attributes4Values1Flags(lowerStart, lowerEnd, lowerStart, upperStart, upperEnd, upperStart,
				parseData, 0, value, extendedValue, value2, extendedValue2,
				keyRangeWildcard|85|flags, 85)
			ipAddressParseData.setBase85(true)
			return true, nil
		}
	}
	return false, nil
}

func chooseMACAddressProvider(fromString *MACAddressString,
	validationOptions addrstrparam.MACAddressStringParams, pa *parsedMACAddress,
	addressParseData *addressParseData) (res macAddressProvider, err addrerr.AddressStringError) {
	if addressParseData.isProvidingEmpty() {
		if validationOptions == defaultMACAddrParameters {
			res = defaultMACAddressEmptyProvider
		} else {
			res = macAddressEmptyProvider{macAddressNullProvider{validationOptions}}
		}
	} else if addressParseData.isAll() {
		if validationOptions == defaultMACAddrParameters {
			res = macAddressDefaultAllProvider
		} else {
			res = &macAddressAllProvider{validationOptions: validationOptions, creationLock: &sync.Mutex{}}
		}
	} else {
		if err = checkMACSegments(fromString.str, validationOptions, pa); err == nil {
			res = pa
		}
	}
	return
}

var maskCache = [3][IPv6BitCount + 1]*maskCreator{}

var loopbackCache = newEmptyAddrCreator(defaultIPAddrParameters, NoZone)

func chooseIPAddressProvider(
	originator HostIdentifierString,
	fullAddr string,
	validationOptions addrstrparam.IPAddressStringParams,
	parseData *parsedIPAddress) (res ipAddressProvider, err addrerr.AddressStringError) {
	qualifier := parseData.getQualifier()
	version := parseData.getProviderIPVersion()
	if version.IsIndeterminate() {
		version = qualifier.inferVersion(validationOptions) // checks whether a mask, prefix length, or zone makes the version clear
		optionsVersion := inferVersion(validationOptions)   // checks whether IPv4 or IPv6 is disallowed
		if version.IsIndeterminate() {
			version = optionsVersion
			parseData.setVersion(version)
		} else if !optionsVersion.IsIndeterminate() && version != optionsVersion {
			var key string
			if version.IsIPv6() {
				key = "ipaddress.error.ipv6"
			} else {
				key = "ipaddress.error.ipv4"
			}
			err = &addressStringError{addressError{str: fullAddr, key: key}}
			return
		}
		addressParseData := parseData.getAddressParseData()
		if addressParseData.isProvidingEmpty() {
			networkPrefixLength := qualifier.getNetworkPrefixLen()
			if networkPrefixLength != nil {
				if version.IsIndeterminate() {
					version = IPVersion(validationOptions.GetPreferredVersion())
				}
				prefLen := networkPrefixLength.bitCount()
				if validationOptions == defaultIPAddrParameters && prefLen <= IPv6BitCount {
					index := 0
					if version.IsIPv4() {
						index = 1
					} else if version.IsIPv6() {
						index = 2
					}
					creator := (*maskCreator)(atomicLoadPointer((*unsafe.Pointer)(unsafe.Pointer(&maskCache[index][prefLen]))))
					if creator == nil {
						creator = newMaskCreator(defaultIPAddrParameters, version, networkPrefixLength)
						dataLoc := (*unsafe.Pointer)(unsafe.Pointer(&maskCache[index][prefLen]))
						atomic.StorePointer(dataLoc, unsafe.Pointer(creator))
					}
					res = creator
					return
				}
				res = newMaskCreator(validationOptions, version, networkPrefixLength)
				return
			} else {
				emptyOpt := validationOptions.EmptyStrParsedAs()
				if emptyOpt == addrstrparam.LoopbackOption || emptyOpt == addrstrparam.ZeroAddressOption {
					result := getLoopbackCreator(validationOptions, qualifier)
					if result != nil {
						res = result
					}
					return
				}

				if validationOptions == defaultIPAddrParameters {
					res = emptyProvider
				} else {
					res = &nullProvider{isEmpty: true, ipType: emptyType, params: validationOptions}
				}
				return
			}
		} else { //isAll
			// Before reaching here, we already checked whether we allow "all", "allowAll".
			if version.IsIndeterminate() && // version not inferred, nor is a particular version disallowed
				validationOptions.AllStrParsedAs() == addrstrparam.AllPreferredIPVersion {
				preferredVersion := IPVersion(validationOptions.GetPreferredVersion())
				if !preferredVersion.IsIndeterminate() {
					var formatParams addrstrparam.IPAddressStringFormatParams
					if preferredVersion.IsIPv6() {
						formatParams = validationOptions.GetIPv6Params()
					} else {
						formatParams = validationOptions.GetIPv4Params()
					}
					if formatParams.AllowsWildcardedSeparator() {
						version = preferredVersion
					}
				}
			}
			res = newAllCreator(qualifier, version, originator, validationOptions)
			return
		}
	} else {
		if parseData.isZoned() && version.IsIPv4() {
			err = &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.only.ipv6.has.zone"}}
			return
		}
		if err = checkSegments(fullAddr, validationOptions, parseData.getIPAddressParseData()); err == nil {
			res = parseData
		}
	}
	return
}

func inferVersion(params addrstrparam.IPAddressStringParams) IPVersion {
	if params.AllowsIPv6() {
		if !params.AllowsIPv4() {
			return IPv6
		}
	} else if params.AllowsIPv4() {
		return IPv4
	}
	return IndeterminateIPVersion
}

func getLoopbackCreator(validationOptions addrstrparam.IPAddressStringParams, qualifier *parsedHostIdentifierStringQualifier) (res *emptyAddrCreator) {
	zone := qualifier.getZone()
	defaultParams := defaultIPAddrParameters
	if validationOptions == defaultParams && zone == NoZone {
		res = loopbackCache
		if res == nil {
			res = newEmptyAddrCreator(defaultIPAddrParameters, NoZone)
		}
		return
	}
	res = newEmptyAddrCreator(validationOptions, zone)
	return
}

func checkSegmentMaxValues(
	fullAddr string,
	parseData *addressParseData,
	segmentIndex int,
	params addrstrparam.AddressStringFormatParams,
	maxValue uint64,
	maxDigitCount,
	maxUpperDigitCount int) addrerr.AddressStringError {
	if parseData.getFlag(segmentIndex, keySingleWildcard) {
		value := parseData.getValue(segmentIndex, keyLower)
		if value > maxValue {
			return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.ipv4.segment.too.large"}}
		}
		if parseData.getValue(segmentIndex, keyUpper) > maxValue {
			parseData.setValue(segmentIndex, keyUpper, maxValue)
		}
		if !params.AllowsUnlimitedLeadingZeros() {
			lowerRadix := parseData.getRadix(segmentIndex, keyLowerRadixIndex)
			if parseData.getIndex(segmentIndex, keyLowerStrEndIndex)-parseData.getIndex(segmentIndex, keyLowerStrDigitsIndex)-getStringPrefixCharCount(lowerRadix) > maxDigitCount {
				return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.segment.too.long"}}
			}
		}
	} else {
		value := parseData.getValue(segmentIndex, keyUpper)
		if value > maxValue {
			return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.ipv4.segment.too.large"}}
		}
		if !params.AllowsUnlimitedLeadingZeros() {
			lowerRadix := parseData.getRadix(segmentIndex, keyLowerRadixIndex)
			lowerEndIndex := parseData.getIndex(segmentIndex, keyLowerStrEndIndex)
			upperEndIndex := parseData.getIndex(segmentIndex, keyUpperStrEndIndex)
			if lowerEndIndex-parseData.getIndex(segmentIndex, keyLowerStrDigitsIndex)-getStringPrefixCharCount(lowerRadix) > maxDigitCount {
				return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.segment.too.long"}}
			}
			if lowerEndIndex != upperEndIndex {
				upperRadix := parseData.getRadix(segmentIndex, keyUpperRadixIndex)
				if upperEndIndex-parseData.getIndex(segmentIndex, keyUpperStrDigitsIndex)-getStringPrefixCharCount(upperRadix) > maxUpperDigitCount {
					return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.segment.too.long"}}
				}
			}
		}
	}
	return nil
}

func checkMACSegments(
	fullAddr string,
	validationOptions addrstrparam.MACAddressStringParams,
	parseData *parsedMACAddress) addrerr.AddressStringError {
	var err addrerr.AddressStringError
	format := parseData.getFormat()
	if format != unknownFormat {
		addressParseData := parseData.getAddressParseData()
		hasWildcardSeparator := addressParseData.hasWildcard() && validationOptions.GetFormatParams().AllowsWildcardedSeparator()
		//note that too many segments is checked inside the general parsing method
		segCount := addressParseData.getSegmentCount()
		if format == dotted {
			if segCount <= MediaAccessControlDottedSegmentCount && validationOptions.GetPreferredLen() != addrstrparam.EUI64Len {
				if !hasWildcardSeparator && segCount != MediaAccessControlDottedSegmentCount {
					return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.too.few.segments"}}
				}
			} else if !hasWildcardSeparator && segCount < MediaAccessControlDotted64SegmentCount {
				return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.too.few.segments"}}
			} else {
				parseData.setExtended(true)
			}
		} else if segCount > 2 {
			if segCount <= MediaAccessControlSegmentCount && validationOptions.GetPreferredLen() != addrstrparam.EUI64Len {
				if !hasWildcardSeparator && segCount != MediaAccessControlSegmentCount {
					return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.too.few.segments"}}
				}
			} else if !hasWildcardSeparator && segCount < ExtendedUniqueIdentifier64SegmentCount {
				return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.too.few.segments"}}
			} else {
				parseData.setExtended(true)
			}
			// we do not check char counts in the main parsing code for dashed, because we allow both
			// aabbcc-ddeeff and aa-bb-cc-dd-ee-ff, so we defer to the check until here
			if parseData.getFormat() == dashed {
				for i := 0; i < segCount; i++ {
					err = checkSegmentMaxValues(
						fullAddr,
						addressParseData,
						i,
						validationOptions.GetFormatParams(),
						MACMaxValuePerSegment,
						MACSegmentMaxChars,
						MACSegmentMaxChars)
					if err != nil {
						return err
					}
				}
			}
		} else {
			if parseData.getFormat() == dashed {
				//for single segment, we have already counted the exact number of hex digits
				//for double segment, we have already counted the exact number of hex digits in some cases and not others.
				//Basically, for address like a-b we have already counted the exact number of hex digits,
				//for addresses starting with a|b- or a-b| we have not,
				//but rather than figure out which are checked and which are not it's just as quick to check them all here
				if parseData.isDoubleSegment() {
					params := validationOptions.GetFormatParams()
					err = checkSegmentMaxValues(fullAddr, addressParseData, 0, params, macMaxTriple, macDoubleSegmentDigitCount, macDoubleSegmentDigitCount)
					if err != nil {
						return err
					}
					if parseData.isExtended() {
						err = checkSegmentMaxValues(fullAddr, addressParseData, 1, params, macMaxQuintuple, macExtendedDoubleSegmentDigitCount, macExtendedDoubleSegmentDigitCount)
					} else {
						err = checkSegmentMaxValues(fullAddr, addressParseData, 1, params, macMaxTriple, macDoubleSegmentDigitCount, macDoubleSegmentDigitCount)
					}
					if err != nil {
						return err
					}
				}
			} else if !hasWildcardSeparator {
				return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.too.few.segments"}}
			}
			if validationOptions.GetPreferredLen() == addrstrparam.EUI64Len {
				parseData.setExtended(true)
			}
		}
	} //else single segment
	return nil
}

func checkSegments(
	fullAddr string,
	validationOptions addrstrparam.IPAddressStringParams,
	parseData *ipAddressParseData) addrerr.AddressStringError {
	addressParseData := parseData.getAddressParseData()
	segCount := addressParseData.getSegmentCount()
	version := parseData.getProviderIPVersion()
	if version.IsIPv4() {
		missingCount := IPv4SegmentCount - segCount
		ipv4Options := validationOptions.GetIPv4Params()
		hasWildcardSeparator := addressParseData.hasWildcard() && ipv4Options.AllowsWildcardedSeparator()

		//single segments are handled in the parsing code with the allowSingleSegment setting
		if missingCount > 0 && segCount > 1 {
			if ipv4Options.Allows_inet_aton_joinedSegments() {
				parseData.set_inet_aton_joined(true)
			} else if !hasWildcardSeparator {
				return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.ipv4.too.few.segments"}}
			}
		}

		//here we check whether values are too large
		notUnlimitedLength := !ipv4Options.AllowsUnlimitedLeadingZeros()
		hasMissingSegs := missingCount > 0 && ipv4Options.Allows_inet_aton_joinedSegments()
		for i := 0; i < segCount; i++ {
			var max uint64
			if hasMissingSegs && i == segCount-1 {
				max = getMaxIPv4Value(missingCount + 1)
				if addressParseData.isInferredUpperBoundary(i) {
					parseData.setValue(i, keyUpper, max)
					continue
				}
			} else {
				max = IPv4MaxValuePerSegment
			}
			if parseData.getFlag(i, keySingleWildcard) {
				value := parseData.getValue(i, keyLower)
				if value > max {
					return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.ipv4.segment.too.large"}}
				}
				if parseData.getValue(i, keyUpper) > max {
					parseData.setValue(i, keyUpper, max)
				}
				if notUnlimitedLength {
					lowerRadix := addressParseData.getRadix(i, keyLowerRadixIndex)
					maxDigitCount := getMaxIPv4StringLength(missingCount, lowerRadix)
					if parseData.getIndex(i, keyLowerStrEndIndex)-parseData.getIndex(i, keyLowerStrDigitsIndex)-getStringPrefixCharCount(lowerRadix) > maxDigitCount {
						return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.segment.too.long"}}
					}
				}
			} else {
				value := parseData.getValue(i, keyUpper)
				if value > max {
					return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.ipv4.segment.too.large"}}
				}
				if notUnlimitedLength {
					lowerRadix := addressParseData.getRadix(i, keyLowerRadixIndex)
					maxDigitCount := getMaxIPv4StringLength(missingCount, lowerRadix)
					lowerEndIndex := parseData.getIndex(i, keyLowerStrEndIndex)
					upperEndIndex := parseData.getIndex(i, keyUpperStrEndIndex)
					if lowerEndIndex-parseData.getIndex(i, keyLowerStrDigitsIndex)-getStringPrefixCharCount(lowerRadix) > maxDigitCount {
						return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.segment.too.long"}}
					}
					if lowerEndIndex != upperEndIndex {
						upperRadix := parseData.getRadix(i, keyUpperRadixIndex)
						maxUpperDigitCount := getMaxIPv4StringLength(missingCount, upperRadix)
						if upperEndIndex-parseData.getIndex(i, keyUpperStrDigitsIndex)-getStringPrefixCharCount(upperRadix) > maxUpperDigitCount {
							return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.segment.too.long"}}
						}
					}
				}
			}
		}
	} else {
		totalSegmentCount := segCount
		if parseData.isProvidingMixedIPv6() {
			totalSegmentCount += IPv6MixedReplacedSegmentCount
		}
		hasWildcardSeparator := addressParseData.hasWildcard() && validationOptions.GetIPv6Params().AllowsWildcardedSeparator()
		if !hasWildcardSeparator && totalSegmentCount != 1 && totalSegmentCount < IPv6SegmentCount && !parseData.isCompressed() {
			return &addressStringError{addressError{str: fullAddr, key: "ipaddress.error.too.few.segments"}}
		}
	}
	return nil
}

func checkSingleWildcard(str string, start, end, digitsEnd int, options addrstrparam.AddressStringFormatParams) addrerr.AddressStringError {
	_ = start
	if !options.GetRangeParams().AllowsSingleWildcard() {
		return &addressStringError{addressError{str: str, key: "ipaddress.error.no.single.wildcard"}}
	}
	for k := digitsEnd; k < end; k++ {
		if str[k] != SegmentSqlSingleWildcard {
			return &addressStringError{addressError{str: str, key: "ipaddress.error.single.wildcard.order"}}
		}
	}
	return nil
}

func switchSingleWildcard10(currentValueHex uint64, s string, start, end, numSingleWildcards int, parseData *addressParseData, parsedSegIndex, leadingZeroStartIndex int, options addrstrparam.AddressStringFormatParams) (err addrerr.AddressStringError) {
	digitsEnd := end - numSingleWildcards
	err = checkSingleWildcard(s, start, end, digitsEnd, options)
	if err != nil {
		return
	}
	var lower uint64
	if start < digitsEnd {
		lower, err = switchValue10(currentValueHex, s, digitsEnd-start)
		if err != nil {
			return
		}
	}
	var upper uint64

	switch numSingleWildcards {
	case 1:
		lower *= 10
		upper = lower + 9
	case 2:
		lower *= 100
		upper = lower + 99
	case 3:
		lower *= 1000
		upper = lower + 999
	default:
		power := uint64(math.Pow10(numSingleWildcards))
		lower *= power
		upper = lower + power - 1
	}
	var radix uint32 = 10
	assign6Attributes2Values2Flags(start, end, leadingZeroStartIndex, start, end, leadingZeroStartIndex,
		parseData, parsedSegIndex, lower, upper, keySingleWildcard|radix, radix)
	return
}

func switchSingleWildcard2(currentValueHex uint64, s string, start, end, numSingleWildcards int, parseData *addressParseData, parsedSegIndex, leadingZeroStartIndex int, options addrstrparam.AddressStringFormatParams) (err addrerr.AddressStringError) {
	digitsEnd := end - numSingleWildcards
	err = checkSingleWildcard(s, start, end, digitsEnd, options)
	if err != nil {
		return
	}
	var lower, upper uint64
	if start < digitsEnd {
		lower, err = switchValue2(currentValueHex, s, digitsEnd-start)
		if err != nil {
			return
		}
	} else {
		lower = 0
	}
	lower <<= uint(numSingleWildcards)
	switch numSingleWildcards {
	case 1:
		upper = lower | 0x1
	case 2:
		upper = lower | 0x3
	case 3:
		upper = lower | 0x7
	case 4:
		upper = lower | 0xf
	case 5:
		upper = lower | 0x1f
	case 6:
		upper = lower | 0x3f
	case 7:
		upper = lower | 0x7f
	case 8:
		upper = lower | 0xff
	case 9:
		upper = lower | 0x1ff
	case 10:
		upper = lower | 0x3ff
	case 11:
		upper = lower | 0x7ff
	case 12:
		upper = lower | 0xfff
	case 13:
		upper = lower | 0x1fff
	case 14:
		upper = lower | 0x3fff
	case 15:
		upper = lower | 0x7fff
	case 16:
		upper = lower | 0xffff
	default:
		upper = lower | ^(^uint64(0) << uint(numSingleWildcards))
	}
	var radix uint32 = 2
	assign6Attributes2Values2Flags(start, end, leadingZeroStartIndex, start, end, leadingZeroStartIndex,
		parseData, parsedSegIndex, lower, upper, keySingleWildcard|radix, radix)
	return
}

func switchSingleWildcard8(currentValueHex uint64, s string, start, end, numSingleWildcards int, parseData *addressParseData, parsedSegIndex, leadingZeroStartIndex int, options addrstrparam.AddressStringFormatParams) (err addrerr.AddressStringError) {
	digitsEnd := end - numSingleWildcards
	err = checkSingleWildcard(s, start, end, digitsEnd, options)
	if err != nil {
		return
	}
	var lower, upper uint64
	if start < digitsEnd {
		lower, err = switchValue8(currentValueHex, s, digitsEnd-start)
		if err != nil {
			return
		}
	}
	switch numSingleWildcards {
	case 1:
		lower <<= 3
		upper = lower | 07
	case 2:
		lower <<= 6
		upper = lower | 077
	case 3:
		lower <<= 9
		upper = lower | 0777
	default:
		shift := numSingleWildcards * 3
		lower <<= uint(shift)
		upper = lower | ^(^uint64(0) << uint(shift))
	}
	var radix uint32 = 8
	assign6Attributes2Values2Flags(start, end, leadingZeroStartIndex, start, end, leadingZeroStartIndex,
		parseData, parsedSegIndex, lower, upper, keySingleWildcard|radix, radix)
	return
}

func assignSingleWildcard16(lower uint64, s string, start, end, numSingleWildcards int, parseData *addressParseData, parsedSegIndex, leadingZeroStartIndex int, options addrstrparam.AddressStringFormatParams) (err addrerr.AddressStringError) {
	digitsEnd := end - numSingleWildcards
	err = checkSingleWildcard(s, start, end, digitsEnd, options)
	if err != nil {
		return
	}
	shift := numSingleWildcards << 2
	lower <<= uint(shift)
	upper := lower | ^(^uint64(0) << uint(shift))
	assign6Attributes2Values1Flags(start, end, leadingZeroStartIndex, start, end, leadingZeroStartIndex,
		parseData, parsedSegIndex, lower, upper, keySingleWildcard)
	return
}

func parseSingleSegmentSingleWildcard16(currentValueHex uint64, s string, start, end, numSingleWildcards int, parseData *addressParseData, parsedSegIndex, leadingZeroStartIndex int, options addrstrparam.AddressStringFormatParams) (err addrerr.AddressStringError) {
	digitsEnd := end - numSingleWildcards
	err = checkSingleWildcard(s, start, end, digitsEnd, options)
	if err != nil {
		return
	}
	var upper, lower, extendedLower, extendedUpper uint64
	if numSingleWildcards < longHexDigits {
		midIndex := end - longHexDigits
		lower = parseLong16(s, midIndex, digitsEnd)
		shift := numSingleWildcards << 2
		lower <<= uint(shift)
		upper = lower | ^(^uint64(0) << uint(shift))
		extendedLower = parseLong16(s, start, midIndex)
		extendedUpper = extendedLower
	} else if numSingleWildcards == longHexDigits {
		lower = 0
		upper = 0xffffffffffffffff
		extendedUpper = currentValueHex
		extendedLower = currentValueHex
	} else {
		lower = 0
		upper = 0xffffffffffffffff
		extendedLower = currentValueHex
		shift := (numSingleWildcards - longHexDigits) << 2
		extendedLower <<= uint(shift)
		extendedUpper = extendedLower | ^(^uint64(0) << uint(shift))
	}
	assign6Attributes4Values1Flags(start, end, leadingZeroStartIndex, start, end, leadingZeroStartIndex,
		parseData, parsedSegIndex, lower, extendedLower, upper, extendedUpper, keySingleWildcard)
	return
}

func parseSingleSegmentSingleWildcard2(s string, start, end, numSingleWildcards int, parseData *addressParseData, parsedSegIndex, leadingZeroStartIndex int, options addrstrparam.AddressStringFormatParams) (err addrerr.AddressStringError) {
	digitsEnd := end - numSingleWildcards
	err = checkSingleWildcard(s, start, end, digitsEnd, options)
	if err != nil {
		return
	}
	var upper, lower, extendedLower, extendedUpper uint64
	midIndex := end - longBinaryDigits
	if numSingleWildcards < longBinaryDigits {
		lower = parseLong2(s, midIndex, digitsEnd)
		shift := numSingleWildcards
		lower <<= uint(shift)
		upper = lower | ^(^uint64(0) << uint(shift))
		extendedLower = parseLong2(s, start, midIndex)
		extendedUpper = extendedLower
	} else if numSingleWildcards == longBinaryDigits {
		upper = 0xffffffffffffffff
		extendedLower = parseLong2(s, start, midIndex)
		extendedUpper = extendedLower
	} else {
		upper = 0xffffffffffffffff
		shift := numSingleWildcards - longBinaryDigits
		extendedLower = parseLong2(s, start, midIndex-shift)
		extendedLower <<= uint(shift)
		extendedUpper = extendedLower | ^(^uint64(0) << uint(shift))
	}
	assign6Attributes4Values1Flags(start, end, leadingZeroStartIndex, start, end, leadingZeroStartIndex,
		parseData, parsedSegIndex, lower, extendedLower, upper, extendedUpper, keySingleWildcard)
	return
}

////////////////////////

var maxValues = [5]uint64{0, IPv4MaxValuePerSegment, 0xffff, 0xffffff, 0xffffffff}

func getMaxIPv4Value(segmentCount int) uint64 {
	return maxValues[segmentCount]
}

func getStringPrefixCharCount(radix uint32) int {
	switch radix {
	case 10:
		return 0
	case 16:
	case 2:
		return 2
	default:
	}
	return 1
}

var maxIPv4StringLen = [9][]int{ //indices are [radix / 2][additionalSegments], and we handle radices 8, 10, 16
	{3, 6, 8, 11},   //no radix supplied we treat as octal, the longest
	{8, 16, 24, 32}, // binary
	{}, {},
	{3, 6, 8, 11},                   //octal: 0377, 0177777, 077777777, 037777777777
	{IPv4SegmentMaxChars, 5, 8, 10}, //decimal: 255, 65535, 16777215, 4294967295
	{}, {},
	{2, 4, 6, 8}, //hex: 0xff, 0xffff, 0xffffff, 0xffffffff
}

func getMaxIPv4StringLength(additionalSegmentsCovered int, radix uint32) int {
	radixHalved := radix >> 1
	if radixHalved < uint32(len(maxIPv4StringLen)) {
		sl := maxIPv4StringLen[radixHalved]
		if additionalSegmentsCovered >= 0 && additionalSegmentsCovered < len(sl) {
			return sl[additionalSegmentsCovered]
		}
	}
	return 0
}

func switchValue2(currentHexValue uint64, s string, digitCount int) (result uint64, err addrerr.AddressStringError) {
	result = 0xf & currentHexValue
	if result > 1 {
		err = &addressStringError{addressError{str: s, key: "ipaddress.error.ipv4.invalid.binary.digit"}}
		return
	}
	shift := 0

	for digitCount--; digitCount > 0; digitCount-- {
		shift++
		currentHexValue >>= 4
		next := 0xf & currentHexValue
		if next >= 1 {
			if next == 1 {
				result |= 1 << uint(shift)
			} else {
				err = &addressStringError{addressError{str: s, key: "ipaddress.error.ipv4.invalid.binary.digit"}}
				return
			}
		}
	}
	return
}

/**
 * The digits were stored as a hex value, this switches them to an octal value.
 *
 * @param currentHexValue
 * @param digitCount
 * @return
 */
func switchValue8(currentHexValue uint64, s string, digitCount int) (result uint64, err addrerr.AddressStringError) {
	result = 0xf & currentHexValue
	if result >= 8 {
		err = &addressStringError{addressError{str: s, key: "ipaddress.error.ipv4.invalid.octal.digit"}}
		return
	}
	shift := 0
	for digitCount--; digitCount > 0; digitCount-- {
		shift += 3
		currentHexValue >>= 4
		next := 0xf & currentHexValue
		if next >= 8 {
			err = &addressStringError{addressError{str: s, key: "ipaddress.error.ipv4.invalid.octal.digit"}}
			return
		}
		result |= next << uint(shift)
	}
	return
}

func switchValue10(currentHexValue uint64, s string, digitCount int) (result uint64, err addrerr.AddressStringError) {
	result = 0xf & currentHexValue
	if result >= 10 {
		err = &addressStringError{addressError{str: s, key: "ipaddress.error.ipv4.invalid.decimal.digit"}}
		return
	}
	digitCount--
	if digitCount > 0 {
		factor := uint64(10)
		for {
			currentHexValue >>= 4
			next := 0xf & currentHexValue
			if next >= 10 {
				err = &addressStringError{addressError{str: s, key: "ipaddress.error.ipv4.invalid.decimal.digit"}}
				return
			}
			result += next * factor
			digitCount--
			if digitCount == 0 {
				break
			}
			if factor == 10 {
				factor = 100
			} else if factor == 100 {
				factor = 1000
			} else {
				factor *= 10
			}
		}
	}
	return
}

func parseLong2(s string, start, end int) uint64 {
	charArray := chars
	result := uint64(charArray[s[start]])
	for start++; start < end; start++ {
		c := s[start]
		if c == '1' {
			result = (result << 1) | 1
		} else {
			result <<= 1
		}
	}
	return result
}

func parseLong8(s string, start, end int) uint64 {
	charArray := chars
	result := uint64(charArray[s[start]])
	for start++; start < end; start++ {
		result = (result << 3) | uint64(charArray[s[start]])
	}
	return result
}

func parseLong10(s string, start, end int) uint64 {
	charArray := chars
	result := uint64(charArray[s[start]])
	for start++; start < end; start++ {
		result = (result * 10) + uint64(charArray[s[start]])
	}
	return result
}

func parseLong16(s string, start, end int) uint64 {
	charArray := chars
	result := uint64(charArray[s[start]])
	for start++; start < end; start++ {
		result = (result << 4) | uint64(charArray[s[start]])
	}
	return result
}

var base85Powers = createBase85Powers()

func createBase85Powers() []big.Int {
	res := make([]big.Int, 10)
	eightyFive := big.NewInt(85)
	res[0].SetUint64(1)
	for i := 1; i < len(res); i++ {
		res[i].Mul(&res[i-1], eightyFive)
	}
	return res
}

func parse85(s string, start, end int) *big.Int {
	charArray := extendedChars
	var result big.Int
	var last bool
	for {
		var partialEnd, power int
		left := end - start
		if last = left <= 9; last {
			partialEnd = end
			power = left
		} else {
			partialEnd = start + 9
			power = 9
		}
		var partialResult = uint64(charArray[s[start]])
		for start++; start < partialEnd; start++ {
			next := charArray[s[start]]
			partialResult = (partialResult * 85) + uint64(next)
		}
		result.Mul(&result, &base85Powers[power]).Add(&result, new(big.Int).SetUint64(partialResult))
		if last {
			break
		}
	}
	return &result
}

//according to rfc 1035 or 952, a label must start with a letter, must end with a letter or digit, and must have in the middle a letter or digit or -
//rfc 1123 relaxed that to allow labels to start with a digit, section 2.1 has a discussion on this.  It states that the highest level component name must be alphabetic - referring to .com or .net or whatever.
//furthermore, the underscore has become generally acceptable, as indicated in rfc 2181
//there is actually a distinction between host names and domain names.  a host name is a specific type of domain name identifying hosts.
//hosts are not supposed to have the underscore.

//en.wikipedia.org/wiki/Domain_Name_System#Domain_name_syntax
//en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names

//max length is 63, cannot start or end with hyphen
//strictly speaking, the underscore is not allowed anywhere, but it seems that rule is sometimes broken
//also, underscores seem to be a part of dns names that are not part of host names, so we allow it here to be safe

//networkadminkb.com/KB/a156/windows-2003-dns-and-the-underscore.aspx

//It's a little confusing.  rfc 2181 https://www.ietf.org/rfc/rfc2181.txt in section 11 on name syntax says that any chars are allowed in dns.
//However, it also says internet host names might have restrictions of their own, and this was defined in rfc 1035.
//rfc 1035 defines the restrictions on internet host names, in section 2.3.1 http://www.ietf.org/rfc/rfc1035.txt

//So we will follow rfc 1035 and in addition allow the underscore.

var (
	ipvFutureUppercase = byte(unicode.ToUpper(rune(IPvFuture)))
	defaultEmptyHost   = &parsedHost{}
)

func (strValidator) validateHostName(fromHost *HostName, validationOptions addrstrparam.HostNameParams) (psdHost *parsedHost, err addrerr.HostNameError) {
	str := fromHost.str
	addrLen := len(str)
	if addrLen > maxHostLength {
		if addrLen > maxHostLength+1 || str[maxHostLength] != LabelSeparator {
			err = &hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid.length"}}
			return
		}
	}
	var segmentUppercase, isNotNormalized, squareBracketed,
		tryIPv6, tryIPv4,
		isPrefixed, hasPortOrService, hostIsEmpty bool
	isAllDigits, isPossiblyIPv6, isPossiblyIPv4 := true, true, true
	isSpecialOnlyIndex, qualifierIndex, index, lastSeparatorIndex := -1, -1, -1, -1
	labelCount := 0
	maxLocalLabels := 6 //should be at least 4 to avoid the array for ipv4 addresses
	var separatorIndices []int
	var normalizedFlags []bool

	sep0, sep1, sep2, sep3, sep4, sep5 := -1, -1, -1, -1, -1, -1
	var isUpper0, isUpper1, isUpper2, isUpper3, isUpper4, isUpper5 bool

	var currentChar byte
	for index++; index <= addrLen; index++ {

		//grab the character to evaluate
		if index == addrLen {
			if index == 0 {
				hostIsEmpty = true
				break
			}
			segmentCountMatchesIPv4 :=
				isPossiblyIPv4 &&
					(labelCount+1 == IPv4SegmentCount) ||
					(labelCount+1 < IPv4SegmentCount && isSpecialOnlyIndex >= 0) ||
					(labelCount+1 < IPv4SegmentCount && validationOptions.GetIPAddressParams().GetIPv4Params().Allows_inet_aton_joinedSegments()) ||
					labelCount == 0 && validationOptions.GetIPAddressParams().AllowsSingleSegment()
			if isAllDigits {
				if isPossiblyIPv4 && segmentCountMatchesIPv4 {
					tryIPv4 = true
					break
				}
				isPossiblyIPv4 = false
				if hasPortOrService && isPossiblyIPv6 { //isPossiblyIPv6 is already false if labelCount > 0
					//since it is all digits, it cannot be host, so we set tryIPv6 rather than just isPossiblyIPv6
					tryIPv6 = true
					break
				}
				err = &hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid"}}
				return
			}
			isPossiblyIPv4 = isPossiblyIPv4 && segmentCountMatchesIPv4
			currentChar = LabelSeparator
		} else {
			currentChar = str[index]
		}

		//check that character
		//we break out of the loop if we hit '[', '*', '%' (as zone or wildcard), or ':' that is not interpreted as port (and this is ipv6)
		//we exit the loop prematurely if we hit '/' or ':' interpreted as port
		if currentChar >= 'a' && currentChar <= 'z' {
			if currentChar > 'f' {
				isPossiblyIPv6 = false
				isPossiblyIPv4 = isPossiblyIPv4 && (currentChar == 'x' && validationOptions.GetIPAddressParams().GetIPv4Params().Allows_inet_aton_hex())
			} else if currentChar == 'b' {
				isPossiblyIPv4 = isPossiblyIPv4 && validationOptions.GetIPAddressParams().GetIPv4Params().AllowsBinary()
			}
			isAllDigits = false
		} else if currentChar >= '0' && currentChar <= '9' {
			//nothing to do
			continue
		} else if currentChar >= 'A' && currentChar <= 'Z' {
			if currentChar > 'F' {
				isPossiblyIPv6 = false
				isPossiblyIPv4 = isPossiblyIPv4 && (currentChar == 'X' && validationOptions.GetIPAddressParams().GetIPv4Params().Allows_inet_aton_hex())
			} else if currentChar == 'B' {
				isPossiblyIPv4 = isPossiblyIPv4 && validationOptions.GetIPAddressParams().GetIPv4Params().AllowsBinary()
			}
			segmentUppercase = true
			isAllDigits = false
		} else if currentChar == LabelSeparator {
			length := index - lastSeparatorIndex - 1
			if length > maxLabelLength {
				err = &hostNameError{addressError{str: str, key: "ipaddress.error.segment.too.long"}}
				return
			} else if length == 0 {
				if index < addrLen {
					err = &hostNameError{addressError{str: str, key: "ipaddress.host.error.segment.too.short"}}
					return
				}
				isPossiblyIPv4 = false
				isNotNormalized = true
			} else {
				if labelCount < maxLocalLabels {
					if labelCount < 3 {
						if labelCount == 0 {
							sep0 = index
							isUpper0 = segmentUppercase
						} else if labelCount == 1 {
							sep1 = index
							isUpper1 = segmentUppercase
						} else {
							sep2 = index
							isUpper2 = segmentUppercase
						}
					} else {
						if labelCount == 3 {
							sep3 = index
							isUpper3 = segmentUppercase
						} else if labelCount == 4 {
							sep4 = index
							isUpper4 = segmentUppercase
						} else {
							sep5 = index
							isUpper5 = segmentUppercase
						}
					}
					labelCount++
				} else if labelCount == maxLocalLabels {
					separatorIndices = make([]int, maxHostSegments+1)
					separatorIndices[labelCount] = index
					if validationOptions.NormalizesToLowercase() {
						normalizedFlags = make([]bool, maxHostSegments+1)
						normalizedFlags[labelCount] = !segmentUppercase
						isNotNormalized = isNotNormalized || segmentUppercase
					}
					labelCount++
				} else {
					separatorIndices[labelCount] = index
					if normalizedFlags != nil {
						normalizedFlags[labelCount] = !segmentUppercase
						isNotNormalized = isNotNormalized || segmentUppercase
					}
					labelCount++
					if labelCount > maxHostSegments {
						err = &hostNameError{addressError{str: str, key: "ipaddress.host.error.too.many.segments"}}
						return
					}
				}
				segmentUppercase = false //this is per segment so reset it
			}
			lastSeparatorIndex = index
			isPossiblyIPv6 = isPossiblyIPv6 && (index == addrLen) //A '.' means not ipv6 (if we see ':' we jump out of loop so mixed address not possible), but for single segment we end up here even without a '.' character in the string
		} else if currentChar == '_' { //this is not supported in host names but is supported in domain names, see discussion in HostName
			isAllDigits = false
		} else if currentChar == '-' {
			//host name segments cannot end with '-'
			if index == lastSeparatorIndex+1 || index == addrLen-1 || str[index+1] == LabelSeparator {
				err = &hostNameIndexError{hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid.character.at.index"}}, index}
				return
			}
			isAllDigits = false
		} else if currentChar == IPv6StartBracket {
			if index == 0 && labelCount == 0 && addrLen > 2 {
				squareBracketed = true
				break
			}
			err = &hostNameIndexError{hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid.character.at.index"}}, index}
			return
		} else if currentChar == PrefixLenSeparator {
			isPrefixed = true
			qualifierIndex = index + 1
			addrLen = index
			isNotNormalized = true
			index--
		} else {
			a := currentChar == SegmentWildcard
			if a || currentChar == SegmentSqlSingleWildcard {
				b := !a
				addressOptions := validationOptions.GetIPAddressParams()
				if b && addressOptions.GetIPv6Params().AllowsZone() { //if we allow zones, we treat '%' as a zone and not as a wildcard
					if isPossiblyIPv6 && labelCount < IPv6SegmentCount {
						tryIPv6 = true
						isPossiblyIPv4 = false
						break
					}
					err = &hostNameIndexError{hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid.character.at.index"}}, index}
					return
				} else {
					if isPossiblyIPv4 {
						if addressOptions.GetIPv4Params().GetRangeParams().AllowsWildcard() {
							if isSpecialOnlyIndex < 0 {
								isSpecialOnlyIndex = index
							}
						} else {
							isPossiblyIPv4 = false
						}
					}
					if isPossiblyIPv6 && addressOptions.GetIPv6Params().GetRangeParams().AllowsWildcard() {
						if isSpecialOnlyIndex < 0 {
							isSpecialOnlyIndex = index
						}
					} else {
						if !isPossiblyIPv4 {
							//needs to be either ipv4 or ipv6
							err = &hostNameIndexError{hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid.character.at.index"}}, index}
							return
						}
						isPossiblyIPv6 = false
					}
				}
				isAllDigits = false
			} else if currentChar == IPv6SegmentSeparator { //also might denote a port
				if validationOptions.AllowsPort() || validationOptions.AllowsService() {
					hasPortOrService = true
					qualifierIndex = index + 1
					addrLen = index //causes loop to terminate, but only after handling the last segment
					isNotNormalized = true
					index--
				} else {
					isPossiblyIPv4 = false
					if isPossiblyIPv6 {
						tryIPv6 = true
						break
					}
					err = &hostNameIndexError{hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid.character.at.index"}}, index}
					return
				}
			} else if currentChar == AlternativeRangeSeparatorStr[0] {
				//} else if currentChar == AlternativeRangeSeparator {
				if index+1 == addrLen {
					err = &hostNameIndexError{hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid.character.at.index"}}, index}
					return
				}
				currentChar = str[index+1]
				if currentChar == AlternativeRangeSeparatorStr[1] {
					isAllDigits = false
					isPossiblyIPv4 = false
					isPossiblyIPv6 = false
					if isSpecialOnlyIndex < 0 {
						isSpecialOnlyIndex = index
					}
					index++
				} else {
					err = &hostNameIndexError{hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid.character.at.index"}}, index}
					return
				}
			} else {
				err = &hostNameIndexError{hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid.character.at.index"}}, index}
				return
			}
		}
	}

	/*
		1. squareBracketed: [ addr ]
		2. tryIPv4 || tryIPv6: this is a string with characters that invalidate it as a host but it still may in fact be an address
			This includes ipv6 strings, ipv4/ipv6 strings with '*', or all dot/digit strings like 1.2.3.4 that are 4 segments
		3. isPossiblyIPv4: this is a string with digits, - and _ characters and the number of separators matches ipv4.  Such strings can also be valid hosts.
		  The range options flag (controlling whether we allow '-' or '_' in addresses) for ipv4 can control whether it is treated as host or address.
		  It also includes "" empty addresses.
		  isPossiblyIPv6: something like f:: or f:1, the former IPv6 and the latter a host "f" with port 1.  Such strings can be valid addresses or hosts.
		  If it parses as an address, we do not treat as host.
	*/
	psdHost = &parsedHost{originalStr: str, params: validationOptions}
	addressOptions := validationOptions.GetIPAddressParams()
	isIPAddress := squareBracketed || tryIPv4 || tryIPv6
	if !validationOptions.AllowsIPAddress() {
		if isIPAddress {
			err = &hostNameError{addressError{str: str, key: "ipaddress.host.error.ipaddress"}}
			return
		}
	} else if isIPAddress || isPossiblyIPv4 || isPossiblyIPv6 {
		provider, addrErr, hostErr := func() (provider ipAddressProvider, addrErr addrerr.AddressError, hostErr addrerr.HostNameError) {
			pa := parsedIPAddress{
				ipAddressParseData: ipAddressParseData{addressParseData: addressParseData{str: str}},
				options:            addressOptions,
				originator:         fromHost,
			}
			hostQualifier := psdHost.getQualifier()
			if squareBracketed {
				//Note:
				//Firstly, we need to find the address end which is denoted by the end bracket
				//Secondly, while zones appear inside bracket, prefix or port appears outside, according to rfc 4038
				//So we keep track of the boolean endsWithPrefix to differentiate.
				endIndex := addrLen - 1
				endsWithQualifier := str[endIndex] != IPv6EndBracket
				if endsWithQualifier {
					for endIndex--; str[endIndex] != IPv6EndBracket; endIndex-- {
						if endIndex == 1 {
							err = &hostNameError{addressError{str: str, key: "ipaddress.host.error.bracketed.missing.end"}}
							return
						}
					}
				}
				startIndex := 1
				if strings.HasPrefix(str[1:], SmtpIPv6Identifier) {
					//SMTP rfc 2821 allows [IPv6:ipv6address]
					startIndex = 6
				} else {
					/* RFC 3986 section 3.2.2
						host = IP-literal / IPv4address / reg-name
						IP-literal = "[" ( IPv6address / IPvFuture  ) "]"
						IPvFuture  = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
					If a URI containing an IP-literal that starts with "v" (case-insensitive),
					indicating that the version flag is present, is dereferenced by an application that does not know the meaning of that version flag,
					then the application should return an appropriate error for "address mechanism not supported".
					*/
					firstChar := str[1]
					if firstChar == IPvFuture || firstChar == ipvFutureUppercase {
						err = &hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid.mechanism"}}
						return
					}
				}
				addrErr = validateIPAddress(addressOptions, str, startIndex, endIndex, pa.getIPAddressParseData(), false)
				if addrErr != nil {
					return
				}
				if endsWithQualifier {
					//here we check what is in the qualifier that follows the bracket: prefix/mask or port?
					//if prefix/mask, we supply the qualifier to the address, otherwise we supply it to the host
					prefixIndex := endIndex + 1
					prefixChar := str[prefixIndex]
					if prefixChar == PrefixLenSeparator {
						isPrefixed = true
					} else if prefixChar == PortSeparator {
						hasPortOrService = true
					} else {
						hostErr = &hostNameIndexError{hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid.character.at.index"}}, prefixIndex}
						return
					}
					qualifierIndex = prefixIndex + 1 //skip the ']/'
					endIndex = len(str)
					addressParseData := pa.getAddressParseData()
					addrErr = parseHostNameQualifier(
						str,
						addressOptions,
						validationOptions,
						hostQualifier,
						isPrefixed,
						hasPortOrService,
						addressParseData.isProvidingEmpty(),
						qualifierIndex,
						endIndex,
						pa.getProviderIPVersion())
					if addrErr != nil {
						return
					}
					insideBracketsQualifierIndex := pa.getQualifierIndex()
					if pa.isZoned() && str[insideBracketsQualifierIndex] == '2' &&
						str[insideBracketsQualifierIndex+1] == '5' {
						//handle %25 from rfc 6874
						insideBracketsQualifierIndex += 2
					}
					addrErr = parseHostAddressQualifier(
						str,
						addressOptions,
						nil,
						pa.hasPrefixSeparator(),
						false,
						pa.getIPAddressParseData(),
						insideBracketsQualifierIndex,
						prefixIndex-1)
					if addrErr != nil {
						return
					}
					if isPrefixed {
						// since we have an address, we apply the prefix to the address rather than to the host
						// rather than use the prefix as a host qualifier, we treat it as an address qualifier and leave the host qualifier as noQualifier
						// also, keep in mind you can combine prefix with zone like fe80::%2/64, see https://tools.ietf.org/html/rfc4007#section-11.7

						// if there are two prefix lengths, we choose the smaller (larger network)
						// if two masks, we combine them (if both network masks, this is the same as choosing smaller prefix)
						addrQualifier := pa.getIPAddressParseData().getQualifier()
						addrErr = addrQualifier.merge(hostQualifier)
						if addrErr != nil {
							return
						}
						hostQualifier.clearPrefixOrMask()
						// note it makes no sense to indicate a port or service with a prefix
					}
				} else {
					qualifierIndex = pa.getQualifierIndex()
					isPrefixed = pa.hasPrefixSeparator()
					hasPortOrService = false
					if pa.isZoned() && str[qualifierIndex] == '2' &&
						str[qualifierIndex+1] == '5' {
						//handle %25 from rfc 6874
						qualifierIndex += 2
					}
					addrErr = parseHostAddressQualifier(str, addressOptions, validationOptions, isPrefixed, hasPortOrService, pa.getIPAddressParseData(), qualifierIndex, endIndex)
					if addrErr != nil {
						return
					}
				}
				//SMTP rfc 2821 allows [ipv4address]
				version := pa.getProviderIPVersion()
				if !version.IsIPv6() && !validationOptions.AllowsBracketedIPv4() {
					err = &hostNameError{addressError{str: str, key: "ipaddress.host.error.bracketed.not.ipv6"}}
					return
				}
			} else { //not square-bracketed
				/*
					there are cases where it can be ipv4 or ipv6, but not many
					any address with a '.' in it cannot be ipv6 at this point (if we hit a ':' first we would have jumped out of the loop)
					any address with a ':' has gone through tests to see if up until that point it could match an ipv4 address or an ipv6 address
					it can only be ipv4 if it has right number of segments, and only decimal digits.
					it can only be ipv6 if it has only hex digits.
					so when can it be both?  if it looks like *: at the start, so that it has the right number of segments for ipv4 but does not have a '.' invalidating ipv6
					so in that case we might have either something like *:1 for it to be ipv4 (ambiguous is treated as ipv4) or *:f:: to be ipv6
					So we validate the potential port (or ipv6 segment) to determine which one and then go from there
					Also, if it is single segment address that is all decimal digits.
				*/

				// We start by checking if there is potentially a port or service
				// if IPv6, we may need to try a :x as a port or service and as a trailing segment
				firstTrySucceeded := false
				hasAddressPortOrService := false
				addressQualifierIndex := -1
				isPotentiallyIPv6 := isPossiblyIPv6 || tryIPv6
				if isPotentiallyIPv6 {
					//find the last port separator, currently we point to the first one with qualifierIndex
					//note that the service we find here could be the ipv4 part of either an ipv6 address or ipv6 mask like this 1:2:3:4:5:6:1.2.3.4 or 1:2:3:4:5:6:1.2.3.4/1:2:3:4:5:6:1.2.3.4
					if !isPrefixed && (validationOptions.AllowsPort() || validationOptions.AllowsService()) {
						for j := len(str) - 1; j >= 0; j-- {
							c := str[j]
							if c == IPv6SegmentSeparator {
								hasAddressPortOrService = true
								addressQualifierIndex = j + 1
							} else if (c >= '0' && c <= '9') ||
								(c >= 'A' && c <= 'Z') ||
								(c >= 'a' && c <= 'z') ||
								(c == '-') ||
								(c == SegmentWildcard) {
								//see validateHostNamePort for more details on valid ports and service names
								continue
							}
							break
						}
					}
				} else {
					hasAddressPortOrService = hasPortOrService
					addressQualifierIndex = qualifierIndex
				}
				var endIndex int
				if hasAddressPortOrService {
					//validate the potential port
					addrErr = parsePortOrService(str, nil, validationOptions, hostQualifier, addressQualifierIndex, len(str))
					if addrErr != nil {
						//certainly not IPv4 since it doesn't qualify as port (see comment above)
						if !isPotentiallyIPv6 {
							//not IPv6 either, so we're done with checking for address
							return
						}
						// no need to call hostQualifier.clear() since parsePortOrService does not populate qualifier on error
						endIndex = len(str)
					} else if isPotentiallyIPv6 {
						//here it can be either a port or part of an IPv6 address, like this: fe80::6a05:caff:fe3:123
						expectPort := validationOptions.ExpectsPort()
						if expectPort {
							//try with port first, then try as IPv6 no port
							endIndex = addressQualifierIndex - 1
						} else {
							//try as IPv6 with no port first, try with port second
							endIndex = len(str)
						}
						//first try
						addrErr = validateIPAddress(addressOptions, str, 0, endIndex, pa.getIPAddressParseData(), false)
						if addrErr == nil {
							// Since no square brackets, we parse as an address (this can affect how zones are parsed).
							// Also, an address cannot end with a single ':' like a port, so we cannot take a shortcut here and parse for port, we must strip it off first (hence no host parameters passed)
							addrErr = parseAddressQualifier(str, addressOptions, nil, pa.getIPAddressParseData(), endIndex)
						}
						if firstTrySucceeded = addrErr == nil; !firstTrySucceeded {
							pa = parsedIPAddress{
								ipAddressParseData: ipAddressParseData{addressParseData: addressParseData{str: str}},
								options:            addressOptions,
								originator:         fromHost,
							}
							if expectPort {
								// we tried with port first, now we try as IPv6 no port
								hostQualifier.clearPortOrService()
								endIndex = len(str)
							} else {
								// we tried as IPv6 with no port first, now we try with port second
								endIndex = addressQualifierIndex - 1
							}
						} else if !expectPort {
							// it is an address
							// we tried with no port and succeeded, so clear the port, it was not a port
							hostQualifier.clearPortOrService()
						}
					} else {
						endIndex = addressQualifierIndex - 1
					}
				} else {
					endIndex = len(str)
				}
				if !firstTrySucceeded {
					if addrErr = validateIPAddress(addressOptions, str, 0, endIndex, pa.getIPAddressParseData(), false); addrErr == nil {
						//since no square brackets, we parse as an address (this can affect how zones are parsed)
						//Also, an address cannot end with a single ':' like a port, so we cannot take a shortcut here and parse for port, we must strip it off first (hence no host parameters passed)
						addrErr = parseAddressQualifier(str, addressOptions, nil, pa.getIPAddressParseData(), endIndex)
					}
					if addrErr != nil {
						return
					}
				}
			}
			// we successfully parsed an IP address
			provider, addrErr = chooseIPAddressProvider(fromHost, str, addressOptions, &pa)
			return
		}()
		if hostErr != nil {
			err = hostErr
			return
		}
		if addrErr != nil {
			if isIPAddress {
				err = &hostAddressNestedError{nested: addrErr}
				return
			}
			psdHost.labelsQualifier.clearPortOrService()
			//fall though and evaluate as a host
		} else {
			psdHost.embeddedAddress.addressProvider = provider
			return
		}
	}

	hostQualifier := psdHost.getQualifier()
	addrErr := parseHostNameQualifier(
		str,
		addressOptions,
		validationOptions,
		hostQualifier,
		isPrefixed,
		hasPortOrService,
		hostIsEmpty,
		qualifierIndex,
		len(str),
		IndeterminateIPVersion)
	if addrErr != nil {
		err = &hostAddressNestedError{nested: addrErr}
		return
	}
	if hostIsEmpty {
		if !validationOptions.AllowsEmpty() {
			err = &hostNameError{addressError{str: str, key: "ipaddress.host.error.empty"}}
			return
		}
		if *hostQualifier == defaultEmptyHost.labelsQualifier {
			psdHost = defaultEmptyHost
		}
	} else {
		if labelCount <= maxLocalLabels {
			maxLocalLabels = labelCount
			separatorIndices = make([]int, maxLocalLabels)
			if validationOptions.NormalizesToLowercase() {
				normalizedFlags = make([]bool, maxLocalLabels)
			}
		} else if labelCount != len(separatorIndices) {
			trimmedSeparatorIndices := make([]int, labelCount)
			copy(trimmedSeparatorIndices[maxLocalLabels:], separatorIndices[maxLocalLabels:labelCount])
			separatorIndices = trimmedSeparatorIndices
			if normalizedFlags != nil {
				trimmedNormalizedFlags := make([]bool, labelCount)
				copy(trimmedNormalizedFlags[maxLocalLabels:], normalizedFlags[maxLocalLabels:labelCount])
				normalizedFlags = trimmedNormalizedFlags
			}
		}
		for i := 0; i < maxLocalLabels; i++ {
			var nextSep int
			var isUpper bool
			if i < 2 {
				if i == 0 {
					nextSep = sep0
					isUpper = isUpper0
				} else {
					nextSep = sep1
					isUpper = isUpper1
				}
			} else if i < 4 {
				if i == 2 {
					nextSep = sep2
					isUpper = isUpper2
				} else {
					nextSep = sep3
					isUpper = isUpper3
				}
			} else if i == 4 {
				nextSep = sep4
				isUpper = isUpper4
			} else {
				nextSep = sep5
				isUpper = isUpper5
			}
			separatorIndices[i] = nextSep
			if normalizedFlags != nil {
				normalizedFlags[i] = !isUpper
				isNotNormalized = isNotNormalized || isUpper
			}
		}
		//We support a.b.com/24:80 (prefix and port combo)
		//or just port, or a service where-ever a port can appear
		//A prefix with port can mean a subnet of addresses using the same port everywhere (the subnet being the prefix block of the resolved address),
		//or just denote the prefix length of the resolved address along with a port

		//here we check what is in the qualifier that follows the bracket: prefix/mask or port?
		//if prefix/mask, we supply the qualifier to the address, otherwise we supply it to the host
		//also, it is possible the address has a zone
		var addrQualifier *parsedHostIdentifierStringQualifier
		if isPrefixed {
			addrQualifier = hostQualifier
		} else {
			addrQualifier = noQualifier
		}
		embeddedAddr := checkSpecialHosts(str, addrLen, addrQualifier)
		hasEmbeddedAddr := embeddedAddr.addressProvider != nil
		if isSpecialOnlyIndex >= 0 && (!hasEmbeddedAddr || embeddedAddr.addressStringError != nil) {
			if embeddedAddr.addressStringError != nil {
				err = &hostAddressNestedError{
					hostNameIndexError: hostNameIndexError{
						hostNameError: hostNameError{
							addressError{str: str, key: "ipaddress.host.error.invalid.character.at.index"},
						},
						index: isSpecialOnlyIndex,
					},
					nested: embeddedAddr.addressStringError,
				}
				return
			}
			err = &hostNameIndexError{hostNameError{addressError{str: str, key: "ipaddress.host.error.invalid.character.at.index"}}, isSpecialOnlyIndex}
			return
		}
		psdHost.separatorIndices = separatorIndices
		psdHost.normalizedFlags = normalizedFlags
		if !hasEmbeddedAddr {
			if !isNotNormalized {
				normalizedLabels := make([]string, len(separatorIndices))
				for i, lastSep := 0, -1; i < len(normalizedLabels); i++ {
					index := separatorIndices[i]
					normalizedLabels[i] = str[lastSep+1 : index]
					lastSep = index
				}
				psdHost.parsedHostCache = &parsedHostCache{
					host:             str,
					normalizedLabels: normalizedLabels,
				}
			}
		} else {
			if isPrefixed {
				psdHost.labelsQualifier.clearPrefixOrMask()
			}
			psdHost.embeddedAddress = embeddedAddr
		}
	}
	return
}

var defaultUncOpts = new(addrstrparam.IPAddressStringParamsBuilder).
	AllowIPv4(false).AllowEmpty(false).AllowMask(false).AllowPrefix(false).ToParams()

var reverseDNSIPv4Opts = new(addrstrparam.IPAddressStringParamsBuilder).
	AllowIPv6(false).AllowEmpty(false).AllowMask(false).AllowPrefix(false).
	GetIPv4AddressParamsBuilder().Allow_inet_aton(false).GetParentBuilder().ToParams()

var reverseDNSIPv6Opts = new(addrstrparam.IPAddressStringParamsBuilder).
	AllowIPv4(false).AllowEmpty(false).AllowMask(false).AllowPrefix(false).
	GetIPv6AddressParamsBuilder().AllowMixed(false).AllowZone(false).GetParentBuilder().ToParams()

func checkSpecialHosts(str string, addrLen int, hostQualifier *parsedHostIdentifierStringQualifier) (emb embeddedAddress) {
	suffix := IPv6UncSuffix
	//note that by using addrLen we are omitting any terminating prefix
	if addrLen > len(suffix) {
		suffixStartIndex := addrLen - len(suffix)
		//get the address for the UNC IPv6 host
		if strings.EqualFold(str[suffixStartIndex:suffixStartIndex+len(suffix)], suffix) {
			var builder strings.Builder
			beginStr := str[:suffixStartIndex]
			foundZone := false
			for i := 0; i < len(beginStr); i++ {
				c := beginStr[i]
				if c == IPv6UncSegmentSeparator {
					c = IPv6SegmentSeparator
				} else if c == IPv6UncRangeSeparatorStr[0] {
					if i+1 < len(beginStr) {
						c = beginStr[i+1]
						if c == IPv6UncRangeSeparatorStr[1] {
							c = RangeSeparator
							i++
						}
					}
				} else if c == IPv6UncZoneSeparator && !foundZone {
					foundZone = true
					c = IPv6ZoneSeparator
				}
				builder.WriteByte(c)
			}
			emb = embeddedAddress{
				isUNCIPv6Literal: true,
			}
			pa := parsedIPAddress{
				options:            defaultUncOpts,
				ipAddressParseData: ipAddressParseData{addressParseData: addressParseData{str: str}},
			}
			var err addrerr.AddressStringError
			addrStr := builder.String()
			addrStrLen := len(addrStr)
			if err = validateIPAddress(defaultUncOpts, addrStr, 0, addrStrLen, pa.getIPAddressParseData(), false); err == nil {
				if err = parseAddressQualifier(addrStr, defaultUncOpts, nil, pa.getIPAddressParseData(), addrStrLen); err == nil {
					//if err = parseAddressQualifier(str, defaultUncOpts, nil, pa.getIPAddressParseData(), addrStrLen); err == nil {
					if *pa.getQualifier() == *noQualifier {
						*pa.getQualifier() = *hostQualifier
					} else if *hostQualifier != *noQualifier {
						_ = pa.getQualifier().merge(hostQualifier)
					}
					emb.addressProvider, err = chooseIPAddressProvider(nil, str, defaultUncOpts, &pa)
				}
			}
			emb.addressStringError = err
			return
		}
	}

	//Note: could support bitstring labels and support subnets in them, however they appear to be generally unused in the real world
	//RFC 2673
	//arpa: https://www.ibm.com/support/knowledgecenter/SSLTBW_1.13.0/com.ibm.zos.r13.halz002/f1a1b3b1220.htm
	//Also, support partial dns lookups and map then to the associated subnet with prefix length, which I think we may
	//already do for ipv4 but not for ipv6, ipv4 uses the prefix notation d.c.b.a/x but ipv6 uses fewer nibbles
	//on the ipv6 side, would just need to add the proper number of zeros and the prefix length
	suffix3 := IPv6ReverseDnsSuffixDeprecated
	if addrLen > len(suffix3) {
		suffix = IPv4ReverseDnsSuffix
		suffix2 := IPv6ReverseDnsSuffix
		var isIpv4, isMatch bool
		suffixStartIndex := addrLen - len(suffix)
		if isMatch = suffixStartIndex > 0 && strings.EqualFold(str[suffixStartIndex:suffixStartIndex+len(suffix)], suffix); !isMatch {
			suffixStartIndex = addrLen - len(suffix2)
			if isMatch = suffixStartIndex > 0 && strings.EqualFold(str[suffixStartIndex:suffixStartIndex+len(suffix2)], suffix2); !isMatch {
				suffixStartIndex = addrLen - len(suffix3)
				isMatch = suffixStartIndex > 0 && strings.EqualFold(str[suffixStartIndex:suffixStartIndex+len(suffix3)], suffix3)
			}
		} else {
			isIpv4 = true
		}
		if isMatch {
			emb = embeddedAddress{
				isReverseDNS: true,
			}
			var err addrerr.AddressStringError
			var sequence string
			var params addrstrparam.IPAddressStringParams
			if isIpv4 {
				sequence, err = convertReverseDNSIPv4(str, suffixStartIndex)
				params = reverseDNSIPv4Opts
			} else {
				sequence, err = convertReverseDNSIPv6(str, suffixStartIndex)
				params = reverseDNSIPv6Opts
			}
			if err == nil {
				pa := parsedIPAddress{
					options:            params,
					ipAddressParseData: ipAddressParseData{addressParseData: addressParseData{str: sequence}},
				}
				if err = validateIPAddress(params, sequence, 0, len(sequence), pa.getIPAddressParseData(), false); err == nil {
					if err = parseAddressQualifier(str, params, nil, pa.getIPAddressParseData(), len(str)); err == nil {
						pa.qualifier = *hostQualifier
						emb.addressProvider, err = chooseIPAddressProvider(nil, sequence, params, &pa)
					}
				}
			}
			emb.addressStringError = err
		}
	}
	//			//handle TLD host https://tools.ietf.org/html/draft-osamu-v6ops-ipv4-literal-in-url-02
	//			//https://www.ietf.org/proceedings/87/slides/slides-87-v6ops-6.pdf
	//			suffix = ".v4";
	//			if(addrLen > suffix.length() &&
	//					str.regionMatches(true, suffixStartIndex = addrLen - suffix.length(), suffix, 0, suffix.length())) {
	//				//not an rfc, so let's leave it
	//			}
	return
}

//123.2.3.4 is 4.3.2.123.in-addr.arpa.

func convertReverseDNSIPv4(str string, suffixStartIndex int) (string, addrerr.AddressStringError) {
	var builder strings.Builder
	builder.Grow(suffixStartIndex)
	segCount := 0
	j := suffixStartIndex
	for i := suffixStartIndex - 1; i > 0; i-- {
		c1 := str[i]
		if c1 == IPv4SegmentSeparator {
			if j-i <= 1 {
				return "", &addressStringIndexError{
					addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
					i}
			}
			for k := i + 1; k < j; k++ {
				builder.WriteByte(str[k])
			}
			builder.WriteByte(c1)
			j = i
			segCount++
		}
	}
	for k := 0; k < j; k++ {
		builder.WriteByte(str[k])
	}
	if segCount+1 != IPv4SegmentCount {
		return "", &addressStringIndexError{
			addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
			0}
	}
	return builder.String(), nil
}

//4321:0:1:2:3:4:567:89ab would be b.a.9.8.7.6.5.0.4.0.0.0.3.0.0.0.2.0.0.0.1.0.0.0.0.0.0.0.1.2.3.4.IP6.ARPA

func convertReverseDNSIPv6(str string, suffixStartIndex int) (string, addrerr.AddressStringError) {
	var builder strings.Builder
	builder.Grow(suffixStartIndex)
	segCount := 0
	for i := suffixStartIndex - 1; i >= 0; {
		var low, high strings.Builder
		isRange := false
		for j := 0; j < 4; j++ {
			c1 := str[i]
			i--
			if i >= 0 {
				c2 := str[i]
				i--
				if c2 == IPv4SegmentSeparator {
					if c1 == SegmentWildcard {
						isRange = true
						low.WriteByte('0')
						high.WriteByte('f')
					} else {
						if isRange {
							return "", &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
								i + 1}
						}
						low.WriteByte(c1)
						high.WriteByte(c1)
					}
				} else if c2 == RangeSeparator {
					high.WriteByte(c1)
					if i >= 1 {
						c2 = str[i]
						i--
						low.WriteByte(c2)
						isFullRange := (c2 == '0' && c1 == 'f')
						if isRange && !isFullRange {
							return "", &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
								i + 1}
						}
						c2 = str[i]
						i--
						if c2 != IPv4SegmentSeparator {
							return "", &addressStringIndexError{
								addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
								i + 1}
						}
					} else {
						return "", &addressStringIndexError{
							addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
							i}
					}
					isRange = true
				} else {
					return "", &addressStringIndexError{
						addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
						i + 1}
				}
			} else if j < 3 {
				return "", &addressStringIndexError{
					addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
					i + 1}
			} else {
				if c1 == SegmentWildcard {
					isRange = true
					low.WriteByte('0')
					high.WriteByte('f')
				} else {
					if isRange {
						return "", &addressStringIndexError{
							addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
							0}
					}
					low.WriteByte(c1)
					high.WriteByte(c1)
				}
			}
		}
		segCount++
		if builder.Len() > 0 {
			builder.WriteByte(IPv6SegmentSeparator)
		}
		builder.WriteString(low.String())
		if isRange {
			builder.WriteByte(RangeSeparator)
			builder.WriteString(high.String())
		}
	}
	if segCount != IPv6SegmentCount {
		return "", &addressStringIndexError{
			addressStringError{addressError{str: str, key: "ipaddress.error.invalid.character.at.index"}},
			0}
	}
	return builder.String(), nil
}

//
// we need to initialize parsing package variables first before using them, so we put these at the bottom of this file

var zeroIPAddressString = NewIPAddressString("")

var ipv4MappedPrefix = NewIPAddressString("::ffff:0:0/96")