comp.lang.ada
 help / color / mirror / Atom feed
* Oh, no! Ada-strings...
@ 1992-04-24 20:24 Borja TORON ANTONS
  0 siblings, 0 replies; 2+ messages in thread
From: Borja TORON ANTONS @ 1992-04-24 20:24 UTC (permalink / raw)


Brian, 

the problem with ADA-strings is that range is too static. Your "empty string" 
is 0-length, but your variable 'str' is a 100-length string... 

they're different types for the compiler.

The solution is to use 'variable strings' as follows:

==================

Package Var_string is

   type vstring(len: natural:=200) is record
          str : STRING (1..len);
   end record;

   function "&" (s1,s2: in vstring) return vstring;
   function "&" (s1: in vstring; car : in CHARACTER) return vstring;
   function find (todo,sub_str: in vstring) return NATURAL;
   function copy (cadena: in vstring; index, count: in natural) return vstring;
   procedure delete(cadena: in out vstring; index, count: natural);
   function convert(s:string) return vstring;
   function "<" (iz,de: in vstring) return BOOLEAN;
   function ">" (iz,de: in vstring) return BOOLEAN;
end Var_string;


==================

--------------------------------------

==================

package body Var_string is

   function "&" (s1,s2: in vstring) return vstring is

   begin
        return (s1.len+s2.len,s1.str&s2.str);
   end "&";

   function "&" (s1: in vstring; car : in CHARACTER) return vstring is

   begin
        return (s1.len+1,s1.str&car);
   end "&";

   function find (todo,sub_str : in vstring) return NATURAL is

   begin
        for i in 1..todo.len-sub_str.len+1 loop
           if todo.str(i..i+sub_str.len-1)=sub_str.str then
                   return i;
           end if;
        end loop;
        return 0;
   end;

   function copy(cadena: in vstring; index, count: in natural) return vstring i
s
     s:vstring;
     indice:natural:=index;
   begin
      s:=(0,"");
      while (indice<=cadena.len)and(indice<=(index+count-1)) loop
	 s:=s&cadena.str(indice);
	 indice:=indice+1;
      end loop;
      return s;
   end;

   procedure delete(cadena: in out vstring; index, count: natural) is
      final: natural :=index+count;
      sub1, sub2: vstring;
   begin
      sub1:=(index-1, cadena.str(1..index-1));
      if final<cadena.len then
	 sub2:=(cadena.len-count, cadena.str(final..cadena.len));
	 cadena:=sub1&sub2;
      else
	 cadena:= sub1;
      end if;
   end;

   function "<" (iz,de: in vstring) return BOOLEAN is

   begin
        return iz.str<de.str;
   end "<";

   function ">" (iz,de: in vstring) return BOOLEAN is

   begin
        return iz.str>de.str;
   end ">";

   function convert(s:string) return vstring is
   begin
      return(s'last+1-s'first,s);
   end;

end Var_string;
=======================

with this package, if you have:

   str: vstring;

you could do:

   str:=(0,"");

or

   str:=convert("");

It's a bit longer, but it helps! 


Borja TORON
Just an ADA user... 

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

* Re: Oh, no! Ada-strings...
@ 1992-04-29 13:55 psinntp!vitro.com!v7.vitro.com!vaxs09
  0 siblings, 0 replies; 2+ messages in thread
From: psinntp!vitro.com!v7.vitro.com!vaxs09 @ 1992-04-29 13:55 UTC (permalink / raw)


>toron@CCUCVX.UNICAN.ES (Borja TORON ANTONS) writes:
> The solution is to use 'variable strings' as follows:
>
> Package Var_string is
>    type vstring(len: natural:=200) is record
>           str : STRING (1..len);
>    end record;

*heavy sigh*

Most of the time, this won't work.

You are declaring an unconstrained type that can take an amount of storage
between 1 and integer'last (approximately).  The compiler has two reasonable
choices in dealing with objects of this type.

1.  Preallocate the maximum space needed by any object of that type.

2.  Allocate the current size for the object. Re-allocate when the space
    requirement changes.

The tradeoff here is one of speed (1 chews less CPU) against space (2 chews
less memory).  By default, my compiler (VAX Ada) and many others will
choose option 1, pre-allocating your "variable" strings.  In my case,
that's 2 gigabytes of storage for one string.  At run time, an attempt
to allocate a vstring produces a constraint error.

	John Briggs			vaxs09@v7.vitro.com

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

end of thread, other threads:[~1992-04-29 13:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-04-29 13:55 Oh, no! Ada-strings psinntp!vitro.com!v7.vitro.com!vaxs09
  -- strict thread matches above, loose matches on Subject: below --
1992-04-24 20:24 Borja TORON ANTONS

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