comp.lang.ada
 help / color / mirror / Atom feed
* String manupulation (again) - free software
@ 2001-08-23 12:15 Reinert Korsnes
  2001-08-24 17:01 ` Robert Dewar
  0 siblings, 1 reply; 2+ messages in thread
From: Reinert Korsnes @ 2001-08-23 12:15 UTC (permalink / 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



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: String manupulation (again) - free software
  2001-08-23 12:15 String manupulation (again) - free software Reinert Korsnes
@ 2001-08-24 17:01 ` Robert Dewar
  0 siblings, 0 replies; 2+ messages in thread
From: Robert Dewar @ 2001-08-24 17:01 UTC (permalink / raw)


Reinert Korsnes <Reinert.Korsnes@ffi.no> wrote in message news:<9m2sf3$8ru$1@snipp.uninett.no>...

> Below is also a test program for this package.
> (package spec not included).

The above comment seems to indicate that you do not understand the
critical importance of the package spec. In fact for a package like
this, the properly commented spec is far more important than the
(essentially straightforward, if not trivial) implementation. It is
the spec that embodies the real work, which is the interface design.



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-08-24 17:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-23 12:15 String manupulation (again) - free software Reinert Korsnes
2001-08-24 17:01 ` Robert Dewar

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