Natools

Check-in [e266afb5fe]
Login
Overview
Comment:string_escapes: new package with helper functions to escape strings
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e266afb5fe99d6c2301c0bd3e2c6aaa1d51c18ad
User & Date: nat on 2016-10-08 15:38:30
Other Links: manifest | tags
Context
2016-10-09
17:49
tools/smaz: add a statistics to the evaluation output check-in: 7b31b329d7 user: nat tags: trunk
2016-10-08
15:38
string_escapes: new package with helper functions to escape strings check-in: e266afb5fe user: nat tags: trunk
2016-10-07
16:01
tools/smaz: first draft of CLI interface for Evaluate_Dictionary check-in: a27f42e127 user: nat tags: trunk
Changes

Added src/natools-string_escapes.adb version [49b61ffb33].



















































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
------------------------------------------------------------------------------
-- Copyright (c) 2016, Natacha Porté                                        --
--                                                                          --
-- Permission to use, copy, modify, and distribute this software for any    --
-- purpose with or without fee is hereby granted, provided that the above   --
-- copyright notice and this permission notice appear in all copies.        --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES --
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         --
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  --
-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   --
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    --
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  --
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           --
------------------------------------------------------------------------------

package body Natools.String_Escapes is

   subtype Hex_Digit is Natural range 0 .. 15;

   function C_Escape_Hex (C : Character) return String;
      --  Return the string representing C in C-style escaped strings

   function Image (N : Hex_Digit) return Character;
      --  Return upper-case hexadecimal image of a digit



   ------------------------------
   -- Local Helper Subprograms --
   ------------------------------

   function C_Escape_Hex (C : Character) return String is
   begin
      case C is
         when Character'Val (0)  => return "\0";
         when Character'Val (8)  => return "\b";
         when Character'Val (9)  => return "\t";
         when Character'Val (10) => return "\n";
         when Character'Val (11) => return "\f";
         when Character'Val (12) => return "\v";
         when Character'Val (13) => return "\r";
         when Character'Val (34) => return "\""";

         when Character'Val (32) | Character'Val (33)
           | Character'Val (35) .. Character'Val (126) =>
            return String'(1 => C);

         when others =>
            declare
               Code : constant Natural := Character'Pos (C);
            begin
               return "\x" & Image (Code / 16) & Image (Code mod 16);
            end;
      end case;
   end C_Escape_Hex;


   function Image (N : Hex_Digit) return Character is
   begin
      case N is
         when 0 .. 9 =>
            return Character'Val (Character'Pos ('0') + N);
         when 10 .. 15 =>
            return Character'Val (Character'Pos ('A') + N - 10);
      end case;
   end Image;



   ----------------------
   -- Public Interface --
   ----------------------

   function C_Escape_Hex
     (S : String;
      Add_Quotes : Boolean := False)
     return String
   is
      Length : Natural := 0;
      O : Positive := 1;
      Sublength : Natural := 0;
   begin
      for I in S'Range loop
         case S (I) is
            when Character'Val (0) | '"'
              | Character'Val (8) .. Character'Val (13) =>
               Length := Length + 2;
            when Character'Val (32) | Character'Val (33)
              | Character'Val (35) .. Character'Val (126) =>
               Length := Length + 1;
            when others =>
               Length := Length + 4;
         end case;
      end loop;

      if Add_Quotes then
         Length := Length + 2;
      end if;

      return Result : String (1 .. Length) do
         if Add_Quotes then
            O := O + 1;
            Result (Result'First) := '"';
            Result (Result'Last) := '"';
         end if;

         for I in S'Range loop
            O := O + Sublength;

            declare
               Img : constant String := C_Escape_Hex (S (I));
            begin
               Sublength := Img'Length;
               Result (O .. O + Sublength - 1) := Img;
            end;
         end loop;
      end return;
   end C_Escape_Hex;

end Natools.String_Escapes;

Added src/natools-string_escapes.ads version [a3cb74363f].





































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
------------------------------------------------------------------------------
-- Copyright (c) 2016, Natacha Porté                                        --
--                                                                          --
-- Permission to use, copy, modify, and distribute this software for any    --
-- purpose with or without fee is hereby granted, provided that the above   --
-- copyright notice and this permission notice appear in all copies.        --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES --
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         --
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  --
-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   --
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    --
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  --
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           --
------------------------------------------------------------------------------

------------------------------------------------------------------------------
-- Natools.String_Escapes provides escaping and unescaping for Strings in   --
-- various formats.                                                         --
------------------------------------------------------------------------------

package Natools.String_Escapes is
   pragma Pure;

   function C_Escape_Hex
     (S : String;
      Add_Quotes : Boolean := False)
     return String;
      --  Replace non-ASCII-printable characters of S with C-style escapes,
      --  either two-character (like "\n") or hexadecimal four-character
      --  (like "\xC3").
      --  Add double quotes at the beginning and end if Add_Quotes.

end Natools.String_Escapes;