/usr/share/ada/adainclude/gnatcoll/gnatcoll-json-utility.adb is in libgnatcoll1.6-dev 1.6gpl2014-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ------------------------------------------------------------------------------
-- G N A T C O L L --
-- --
-- Copyright (C) 2011-2014, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- --
-- --
-- --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------
with Ada.Integer_Text_IO;
with Ada.Strings.Fixed;
with Ada.Strings.Wide_Unbounded; use Ada.Strings.Wide_Unbounded;
pragma Warnings (Off, "*internal GNAT unit*");
with Ada.Strings.Unbounded.Aux;
with System.Unsigned_Types; use System.Unsigned_Types;
pragma Warnings (On, "*internal GNAT unit*");
with GNAT.Encode_UTF8_String;
with GNAT.Decode_UTF8_String;
package body GNATCOLL.JSON.Utility is
use Ada.Strings.Unbounded;
--------------------------------
-- Escape_Non_Print_Character --
--------------------------------
function Escape_Non_Print_Character (C : Wide_Character) return String is
Int : constant Integer := Wide_Character'Pos (C);
Str : String (1 .. 8);
First, Last : Natural;
begin
Ada.Integer_Text_IO.Put (Str, Int, 16);
First := Ada.Strings.Fixed.Index (Str, "16#") + 3;
Last := Ada.Strings.Fixed.Index (Str, "#", Ada.Strings.Backward) - 1;
-- Make sure we have 4 characters, prefixed with '0's
Str (Last - 3 .. First - 1) := (others => '0');
First := Last - 3;
return "\u" & Str (First .. Last);
end Escape_Non_Print_Character;
-------------------
-- Escape_String --
-------------------
function Escape_String
(Text : UTF8_Unbounded_String) return Unbounded_String
is
use Ada.Strings.Unbounded.Aux;
Str : Big_String_Access;
Text_Length : Natural;
Ret : Unbounded_String;
WS : Unbounded_Wide_String;
Low : Natural;
W_Chr : Wide_Character;
begin
Get_String (Text, Str, Text_Length);
-- First decode the UTF-8 String
Low := 1;
while Low <= Text_Length loop
-- UTF-8 sequence is maximum 4 characters long according to RFC3629
GNAT.Decode_UTF8_String.Decode_Wide_Character
(Str (Low .. Natural'Min (Text_Length, Low + 3)), Low, W_Chr);
Append (WS, W_Chr);
end loop;
Append (Ret, '"');
for J in 1 .. Length (WS) loop
W_Chr := Element (WS, J);
case W_Chr is
when '"' =>
Append (Ret, "\""");
when '\' =>
Append (Ret, "\\");
when Wide_Character'Val (Character'Pos (ASCII.BS)) =>
Append (Ret, "\b");
when Wide_Character'Val (Character'Pos (ASCII.FF)) =>
Append (Ret, "\f");
when Wide_Character'Val (Character'Pos (ASCII.LF)) =>
Append (Ret, "\n");
when Wide_Character'Val (Character'Pos (ASCII.CR)) =>
Append (Ret, "\r");
when Wide_Character'Val (Character'Pos (ASCII.HT)) =>
Append (Ret, "\t");
when others =>
if Wide_Character'Pos (W_Chr) >= 16#80# then
Append (Ret, Escape_Non_Print_Character (W_Chr));
else
Append
(Ret, "" & Character'Val (Wide_Character'Pos (W_Chr)));
end if;
end case;
end loop;
Append (Ret, '"');
return Ret;
end Escape_String;
----------------------
-- Un_Escape_String --
----------------------
function Un_Escape_String
(Text : Unbounded_String;
Low : Natural;
High : Natural) return UTF8_Unbounded_String
is
First : Integer;
Last : Integer;
Unb : Unbounded_String;
Idx : Natural;
begin
First := Low;
Last := High;
-- Trim blanks and double quotes
while First <= High and then Element (Text, First) = ' ' loop
First := First + 1;
end loop;
if First <= High and then Element (Text, First) = '"' then
First := First + 1;
end if;
while Last >= Low and then Element (Text, Last) = ' ' loop
Last := Last - 1;
end loop;
if Last >= Low and then Element (Text, Last) = '"' then
Last := Last - 1;
end if;
Idx := First;
while Idx <= Last loop
if Element (Text, Idx) = '\' then
Idx := Idx + 1;
if Idx > High then
raise Invalid_JSON_Stream with
"Unexpected escape character at end of line";
end if;
-- See http://tools.ietf.org/html/rfc4627 for the list of
-- characters that can be escaped.
case Element (Text, Idx) is
when 'u' | 'U' =>
declare
I : constant Short_Unsigned :=
Short_Unsigned'Value
("16#" & Slice (Text, Idx + 1, Idx + 4) & "#");
begin
Append
(Unb,
GNAT.Encode_UTF8_String.Encode_Wide_String
((1 => Wide_Character'Val (I))));
Idx := Idx + 4;
end;
when '"' =>
Append (Unb, '"');
when '/' =>
Append (Unb, '/');
when '\' =>
Append (Unb, '\');
when 'b' =>
Append (Unb, ASCII.BS);
when 'f' =>
Append (Unb, ASCII.FF);
when 'n' =>
Append (Unb, ASCII.LF);
when 'r' =>
Append (Unb, ASCII.CR);
when 't' =>
Append (Unb, ASCII.HT);
when others =>
raise Invalid_JSON_Stream with
"Unexpected escape sequence '\" &
Element (Text, Idx) & "'";
end case;
else
Append
(Unb, Element (Text, Idx));
end if;
Idx := Idx + 1;
end loop;
return Unb;
end Un_Escape_String;
end GNATCOLL.JSON.Utility;
|