comp.lang.ada
 help / color / mirror / Atom feed
From: Reinert Korsnes <Reinert.Korsnes@ffi.no>
Subject: String manupulation (again) - free software
Date: Thu, 23 Aug 2001 14:15:34 +0200
Date: 2001-08-23T14:15:34+02:00	[thread overview]
Message-ID: <9m2sf3$8ru$1@snipp.uninett.no> (raw)

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



             reply	other threads:[~2001-08-23 12:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-23 12:15 Reinert Korsnes [this message]
2001-08-24 17:01 ` String manupulation (again) - free software Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox