| 1 |
with Ada.Containers.Indefinite_Ordered_Maps; |
| 2 |
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; |
| 3 |
|
| 4 |
with GNAT.Regpat; |
| 5 |
|
| 6 |
package body URI is |
| 7 |
|
| 8 |
package Part_Maps is |
| 9 |
new Ada.Containers.Indefinite_Ordered_Maps (Parts, String); |
| 10 |
|
| 11 |
subtype Part_Map is Part_Maps.Map; |
| 12 |
|
| 13 |
----------- |
| 14 |
-- Crack -- |
| 15 |
----------- |
| 16 |
|
| 17 |
-- ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? |
| 18 |
-- 12 3 4 5 6 7 8 9 |
| 19 |
-- |
| 20 |
-- scheme = $2 |
| 21 |
-- authority = $4 |
| 22 |
-- path = $5 |
| 23 |
-- query = $7 |
| 24 |
-- fragment = $9 |
| 25 |
|
| 26 |
function Crack (This : String) return Part_Map is |
| 27 |
use GNAT.Regpat; |
| 28 |
|
| 29 |
Cracker : constant Pattern_Matcher := |
| 30 |
Compile |
| 31 |
("^\s*(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#([^\s]*))?", |
| 32 |
Case_Insensitive + Single_Line); |
| 33 |
|
| 34 |
Matches : Match_Array (0 .. 9) := (others => No_Match); |
| 35 |
|
| 36 |
function Part (I : Positive) return String |
| 37 |
is (if Matches (I) /= No_Match |
| 38 |
then This (Matches (I).First .. Matches (I).Last) |
| 39 |
else ""); |
| 40 |
|
| 41 |
begin |
| 42 |
Match (Cracker, This, Matches); |
| 43 |
|
| 44 |
return Map : Part_Map do |
| 45 |
Map.Insert (Scheme, Part (2)); |
| 46 |
Map.Insert (Authority, Part (4)); |
| 47 |
Map.Insert (Path, Part (5)); |
| 48 |
Map.Insert (Query, Part (7)); |
| 49 |
Map.Insert (Fragment, Part (9)); |
| 50 |
end return; |
| 51 |
end Crack; |
| 52 |
|
| 53 |
------------- |
| 54 |
-- Extract -- |
| 55 |
------------- |
| 56 |
|
| 57 |
function Extract (This : String; Part : Parts) return String |
| 58 |
is (Extract (This, First => Part, Last => Part)); |
| 59 |
|
| 60 |
------------- |
| 61 |
-- Extract -- |
| 62 |
------------- |
| 63 |
|
| 64 |
function Extract (This : String; First, Last : Parts) return String |
| 65 |
is |
| 66 |
Slice : Unbounded_String; |
| 67 |
Parts : constant Part_Map := Crack (This); |
| 68 |
begin |
| 69 |
for I in First .. Last loop |
| 70 |
if Parts.Contains (I) then |
| 71 |
|
| 72 |
-- prefixes |
| 73 |
|
| 74 |
if I /= First and then Parts (I) /= "" and then Slice /= "" then |
| 75 |
case I is |
| 76 |
when Scheme => null; |
| 77 |
when Authority => Append (Slice, "//"); |
| 78 |
when Path => null; |
| 79 |
when Query => Append (Slice, "?"); |
| 80 |
when Fragment => Append (Slice, "#"); |
| 81 |
end case; |
| 82 |
end if; |
| 83 |
|
| 84 |
Append (Slice, Parts (I)); |
| 85 |
|
| 86 |
-- postfixes |
| 87 |
|
| 88 |
if I = Scheme and then Parts (I) /= "" and then Extract (This, Authority, Last) /= "" then |
| 89 |
Append (Slice, ":"); |
| 90 |
end if; |
| 91 |
|
| 92 |
end if; |
| 93 |
end loop; |
| 94 |
|
| 95 |
return To_String (Slice); |
| 96 |
end Extract; |
| 97 |
|
| 98 |
end URI; |