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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f031433924c1a0a3,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-08-23 05:21:23 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!128.39.3.168!uninett.no!not-for-mail From: Reinert Korsnes Newsgroups: comp.lang.ada Subject: String manupulation (again) - free software Date: Thu, 23 Aug 2001 14:15:34 +0200 Organization: UNINETT news service Message-ID: <9m2sf3$8ru$1@snipp.uninett.no> NNTP-Posting-Host: sthrkou.ffi.no Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: snipp.uninett.no 998569251 9086 193.156.99.159 (23 Aug 2001 12:20:51 GMT) X-Complaints-To: news-abuse@uninett.no User-Agent: KNode/0.4 Xref: archiver1.google.com comp.lang.ada:12321 Date: 2001-08-23T14:15:34+02:00 List-Id: Hi, Enclosed is a simple package to split character strings into substrings consisting of continuous sequences of letters or groups of characters bounded by Quotation (so "abc def, ghijk" is one "word"). Not that this may be so interesting, but it could be useful for me get this improved/criticized. At least I can learn some Ada from this..... Below is also a test program for this package. (package spec not included). reinert ----------------------------------------------- -- Author: R.Korsnes, with help from comp.lang.ada :-) 23 August 2001. with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO,Ada.Strings.Fixed,Ada.Strings.Maps; with Ada.Characters.Latin_1; use Ada.Characters.Latin_1; package body Rsplit is use Ada.Integer_Text_IO; use Ada.Strings,Ada.Strings.Fixed,Ada.Strings.Maps; Set : constant Character_Set := To_Set(" ," & HT); function Number_Of_Words(FS : String) return Integer is N : Natural := 0; First : Natural := FS'First; Last : Natural; begin loop Find_Token(Source => FS(First..FS'Last), Set => Set, Test => Ada.Strings.Outside, First => First, Last => Last); exit when Last = 0; if FS(First) = Quotation then Last := Index(Source => FS(First+1..FS'Last), Set => To_Set(Quotation)); end if; N := N + 1; First := Last + 1; end loop; Return N; end Number_Of_Words; function Word(FS : String;Word_Number : Integer) return String is N : Natural := 0; First : Natural := FS'First; Last : Natural; begin loop Find_Token(Source => FS(First..FS'Last), Set => Set, Test => Ada.Strings.Outside, First => First, Last => Last); exit when Last = 0; if FS(First) = Quotation then Last := Index(Source => FS(First+1..FS'Last), Set => To_Set(Quotation)); end if; N := N + 1; if Word_Number = N then return FS(First .. Last); end if; First := Last+1; end loop; Return ""; end Word; end rsplit; ---------------------------- Test program: with rsplit; with Text_IO,Ada.Strings.Fixed,Ada.Strings.Maps,Ada.Characters.Latin_1; with Ada.Integer_Text_IO; use Text_IO,Ada.Strings,Ada.Strings.Fixed,Ada.Strings.Maps; use Ada.Characters.Latin_1; use Ada.Integer_Text_IO; procedure rstring is FS : constant String := " This " & Quotation & " is a test" & Quotation & " to " & Quotation & "split a string" & Quotation & " "; C1 : constant String := "123456789012345678901234567890123456"; begin Put(C1);New_Line; Put(FS);New_Line; for I in 1 .. Rsplit.Number_Of_Words(FS) loop New_Line; Put(I); Put(" ");Put(" "); Put(Rsplit.Word(FS,I)); end loop; end rstring; -- http://home.chello.no/~rkorsnes