From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ace7fe7a8b92a3bc X-Google-Attributes: gid103376,public From: Florian Weimer Subject: Re: Bounded_String examples/help Date: 2000/04/07 Message-ID: <871z4h7mu1.fsf@deneb.cygnus.argh.org>#1/1 X-Deja-AN: 608137954 References: <8cl557$257$1@nnrp1.deja.com> Mail-Copies-To: never Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@cygnus.argh.org X-Trace: deneb.cygnus.argh.org 955134375 29917 192.168.1.2 (7 Apr 2000 19:06:15 GMT) Organization: Penguin on board User-Agent: Gnus/5.0804 (Gnus v5.8.4) Emacs/20.6 Mime-Version: 1.0 Reply-To: Florian Weimer NNTP-Posting-Date: 7 Apr 2000 19:06:15 GMT Newsgroups: comp.lang.ada Date: 2000-04-07T19:06:15+00:00 List-Id: 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... :-/