Split'n'Join

Christoph Karl Walter Grein

The capability to split strings to form a list and to join list elements to form strings is very useful for text processing applications. This is the sort of thing Perl is famous for.

So I present a child package Ada.Strings.Unbounded.Lists, which I found handy for some code generator application. (This has been inspired by Tash). Note that it is legal to add children to children of Ada, but not to Ada directly. So you can compile the package as is.

But beware, I did not take care to write an efficient implementation. If you like the interface, feel free to implement a more efficient algorithm and send it to me. For my (small) applications, it was fast enough.

The library has the following declaration:


with Ada.Strings.Maps;

package Ada.Strings.Unbounded.Lists is

  type String_List is private;

  Null_String_List: constant String_List;

  function "=" (Left, Right : String_List) return Boolean;

  function Length (Source: String_List) return Natural;

  function To_String_List (Singleton: String          ) return String_List;
  function To_String_List (Singleton: Unbounded_String) return String_List;

  function "&" (Left,                   Right: String_List     ) return String_List;
  function "&" (Left: String          ; Right: String_List     ) return String_List;
  function "&" (Left: String_List     ; Right: String          ) return String_List;
  function "&" (Left: Unbounded_String; Right: String_List     ) return String_List;
  function "&" (Left: String_List     ; Right: Unbounded_String) return String_List;

  function Split (Source  : String;
                  Splitter: Maps.Character_Set) return String_List;
  function Split (Source  : Unbounded_String;
                  Splitter: Maps.Character_Set) return String_List;

  function Join (Source: String_List;
                 Joiner: Character) return String;
  function Join (Source: String_List;
                 Joiner: Character) return Unbounded_String;

  function Element (Source: String_List; Index: Positive) return String;
  function Element (Source: String_List; Index: Positive) return Unbounded_String;

  procedure Set_Element (Source  : in out String_List;
                         Index   : in     Positive;
                         To_Value: in     String);
  procedure Set_Element (Source  : in out String_List;
                         Index   : in     Positive;
                         To_Value: in     Unbounded_String);

  function Pop      (Source: String_List) return String_List;
  function Condense (Source: String_List) return String_List;
  function Slice    (Source: String_List;
                     Low   : Positive;
                     High  : Natural    ) return String_List;

private

  -- not shown

end Ada.Strings.Unbounded.Lists;


The package is released under the GMGPL. Download ready to use package together with RM-like specification.


Inhaltsverzeichnis
Contents