* Bounded_String examples/help
@ 2000-04-07 0:00 ebresie
2000-04-07 0:00 ` Florian Weimer
0 siblings, 1 reply; 2+ messages in thread
From: ebresie @ 2000-04-07 0:00 UTC (permalink / raw)
I am in a situation where it seems a Bounded String would be
appropriately used.
I am wondering if anyone has any examples of a Bounded String?
I am trying something along the lines of
with Ada.Strings.Bounded;
Use Ada.Strings.Bounded;
package Fred is
Message_Max : constant Integer := 1000;
type Message is new Bounded_String( Message_Max);
.
Sent via Deja.com http://www.deja.com/
Before you buy.
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Bounded_String examples/help
2000-04-07 0:00 Bounded_String examples/help ebresie
@ 2000-04-07 0:00 ` Florian Weimer
0 siblings, 0 replies; 2+ messages in thread
From: Florian Weimer @ 2000-04-07 0:00 UTC (permalink / raw)
ebresie@usa.net writes:
> I am in a situation where it seems a Bounded String would be
> appropriately used.
>
> I am wondering if anyone has any examples of a Bounded String?
Hope this helps (the code is under the GPL).
with Ada.Strings.Bounded; use Ada.Strings.Bounded;
function Quote_String (Source : String) return String;
-- Quote the given string in the C way. All funny characters
-- are replaced by "\xxx", where xxx is the octal number of the
-- characater.
function Quote_String (Source : String) return String is
function To_Octal (C : Character) return String;
-- Returns a backslash "\" followed by three octal digits
-- (C notation).
function To_Octal (C : Character) return String
is
subtype Octal_Digit is Integer range 0 .. 7;
function Digit (OD : Octal_Digit) return Character;
function Digit (OD : Octal_Digit) return Character
is
begin
return Character'Val (Character'Pos ('0')
+ OD);
end Digit;
Result : String (1 .. 4);
Remaining : Natural := Character'Pos (C);
begin
Result (4) := Digit (Remaining mod 8);
Remaining := Remaining / 8;
Result (3) := Digit (Remaining mod 8);
Remaining := Remaining / 8;
Result (2) := Digit (Remaining mod 8);
Result (1) := '\';
return Result;
end To_Octal;
package Result_Strings is new Generic_Bounded_Length
(Source'Length * 4);
use Result_Strings;
Result : Bounded_String;
begin
for I in Source'Range loop
case Character'Pos (Source (I)) is
when 32 .. 91 | 93 .. 126 =>
Append (Result, Source (I));
when Character'Pos ('\') =>
Append (Result, "\\");
when others =>
Append (Result, To_Octal (Source (I)));
end case;
end loop;
return To_String (Result);
end Quote_String;
BTW: Are there more lightweight implementations of Ada.Strings.Bounded
available for GNAT? Each instantiation of Generic_Bounded_Length adds
a few kilobytes of code... :-/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2000-04-07 0:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-04-07 0:00 Bounded_String examples/help ebresie
2000-04-07 0:00 ` Florian Weimer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox