comp.lang.ada
 help / color / mirror / Atom feed
* String slicing.
@ 2002-03-17 22:23 Wannabe h4x0r
  2002-03-17 22:43 ` Jim Rogers
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Wannabe h4x0r @ 2002-03-17 22:23 UTC (permalink / raw)


I've written a type definition as follows...

	type wrd_string is new String(1..Ada.Characters.Latin_1.Space);

Now it says that I'm using incompatible types. Now, shouldn't the
compiler be smart enough to recognize that I'm using Characters to count
the characters of an array slice. i.e. a String is nothing more than an
array of characters.

Basically I'm breaking down a text file into chunks of "words" and
"sentences" for sorting by Mats Webers "quick_sort" routine in his
components library. Or rather, I'm learning how to. I can sort by
character easily enough, now I'm learning to work with slices of Strings.

This is a general pain in the ass for me, as it seems Ada wont let me
slice and reference pieces of strings using Characters. Dont worry, I am
reading the manual. Any tips or pointers would be helpful.

Chris



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

* Re: String slicing.
  2002-03-17 22:23 String slicing Wannabe h4x0r
@ 2002-03-17 22:43 ` Jim Rogers
  2002-03-17 23:53   ` Wannabe h4x0r
  2002-03-18 12:44 ` sk
  2002-03-18 17:49 ` Georg Bauhaus
  2 siblings, 1 reply; 6+ messages in thread
From: Jim Rogers @ 2002-03-17 22:43 UTC (permalink / raw)


The compiler is correct here. The index subtype for strings is
Positive. A character is not a member of the Positive subtype.

My guess from your description is that you want the
capabilities defined in the package Ada.Strings.Fixed.
This package will help you scan a string by words.

Jim Rogers

Wannabe h4x0r wrote:

> I've written a type definition as follows...
> 
> 	type wrd_string is new String(1..Ada.Characters.Latin_1.Space);
> 
> Now it says that I'm using incompatible types. Now, shouldn't the
> compiler be smart enough to recognize that I'm using Characters to count
> the characters of an array slice. i.e. a String is nothing more than an
> array of characters.
> 
> Basically I'm breaking down a text file into chunks of "words" and
> "sentences" for sorting by Mats Webers "quick_sort" routine in his
> components library. Or rather, I'm learning how to. I can sort by
> character easily enough, now I'm learning to work with slices of Strings.
> 
> This is a general pain in the ass for me, as it seems Ada wont let me
> slice and reference pieces of strings using Characters. Dont worry, I am
> reading the manual. Any tips or pointers would be helpful.
> 
> Chris
> 




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

* Re: String slicing.
  2002-03-17 22:43 ` Jim Rogers
@ 2002-03-17 23:53   ` Wannabe h4x0r
  0 siblings, 0 replies; 6+ messages in thread
From: Wannabe h4x0r @ 2002-03-17 23:53 UTC (permalink / raw)


On Sun, 17 Mar 2002 17:43:57 -0500, Jim Rogers wrote:

> The compiler is correct here. The index subtype for strings is Positive.
> A character is not a member of the Positive subtype.

	I did not know that.
 
> My guess from your description is that you want the capabilities defined
> in the package Ada.Strings.Fixed. This package will help you scan a
> string by words.
> 
> Jim Rogers

I'll check out the Ada.Strings.Fixed package. Sounds like it might just
be right up my ally.

Thanks.

Chris



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

* Re: String slicing.
  2002-03-17 22:23 String slicing Wannabe h4x0r
  2002-03-17 22:43 ` Jim Rogers
@ 2002-03-18 12:44 ` sk
  2002-03-19  2:36   ` Wannabe h4x0r
  2002-03-18 17:49 ` Georg Bauhaus
  2 siblings, 1 reply; 6+ messages in thread
From: sk @ 2002-03-18 12:44 UTC (permalink / raw)


Hi,

If you are using Gnat, type "gnatpsta" at the command-line and
you will be provided the following :

   type Integer is range -(2 ** 31) .. +(2 ** 31 - 1);

   subtype Natural  is Integer range 0 .. +(2 ** 31 - 1);
   subtype Positive is Integer range 1 .. +(2 ** 31 - 1);

   ...

   type String is array (Positive range <>) of Character;
   pragma Pack
(String);                                                        

So in the simplest terms, a string is a numerically indexed
array of characters.


declare
    -- SB, SE --> String_Begins, String_Ends
    SB, SE : Natural := 0;
    Sample_String : constant String := "Hello Cruel World";

begin
    -- Find first ' '
    for i in Sample_String'Range loop
        if Sample_String (I) = ' ' then
             SB := i;
             exit;
        end if;
    end loop;

    ...
    -- Pretend that the second ' ' has been found.

    TIO.Put_Line (
        "The World is such a " &
        Sample_String (SB .. SE) &
        " place."
     );

end;

In general, unless you are searching for control characters,
specify them in the source or use named characters.

For example,

    Html_Tag_Open  : constant Character := '<';
    Html_Tag_Close : constant Character := '>';

    Horizontal_Tab : constant Character :=
        Ada.Characters.Latin_1.HT;

Also, you don't need to introduce the heavy-weight 
Ada.Strings.xxx unless your application is more
complex than the one above. Even then, I personally
just write small string-searchers within the application
rather than introduce the Ada.Strings.xxx

Hope this helps.

-- 
-------------------------------------
-- Merge vertically for real address
-------------------------------------
s n p @ t . o
 k i e k c c m
-------------------------------------



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

* Re: String slicing.
  2002-03-17 22:23 String slicing Wannabe h4x0r
  2002-03-17 22:43 ` Jim Rogers
  2002-03-18 12:44 ` sk
@ 2002-03-18 17:49 ` Georg Bauhaus
  2 siblings, 0 replies; 6+ messages in thread
From: Georg Bauhaus @ 2002-03-18 17:49 UTC (permalink / raw)


Wannabe h4x0r <chris@dont.spam.me> wrote:
: I've written a type definition as follows...
: 
:        type wrd_string is new String(1..Ada.Characters.Latin_1.Space);

try 'Pos and/or browse Ada.strings.* functionality.

- georg



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

* Re: String slicing.
  2002-03-18 12:44 ` sk
@ 2002-03-19  2:36   ` Wannabe h4x0r
  0 siblings, 0 replies; 6+ messages in thread
From: Wannabe h4x0r @ 2002-03-19  2:36 UTC (permalink / raw)


That makes things a whole lot simpler.

Thanks.

In fact, I could just make a pragma Pure package that 
defines all the tags I'm looking for at the outset.

What types of projects would require(or at least be better
handled) by using Ada.Strings.Fixed rather than using my
own custom routines?

Basically, I'll be using a Hashed Array Tree to store and sort all the
data going into and out of the app.( Dr. Dobbs Journal #326, July 2001,
p. 132). Dont know if this would be appropriate for Ada.Strings.Fixed or
not.

Chris



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

end of thread, other threads:[~2002-03-19  2:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-17 22:23 String slicing Wannabe h4x0r
2002-03-17 22:43 ` Jim Rogers
2002-03-17 23:53   ` Wannabe h4x0r
2002-03-18 12:44 ` sk
2002-03-19  2:36   ` Wannabe h4x0r
2002-03-18 17:49 ` Georg Bauhaus

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