1. -- 
  2. -- Copyright (c) 2013 Kevin Wellwood 
  3. -- All rights reserved. 
  4. -- 
  5. -- This source code is distributed under the Modified BSD License. For terms and 
  6. -- conditions, see license.txt. 
  7. -- 
  8.  
  9. with Interfaces;                        use Interfaces; 
  10. with Interfaces.C;                      use Interfaces.C; 
  11. with Interfaces.C.Strings;              use Interfaces.C.Strings; 
  12.  
  13. -- Allegro 5.0.9 - UTF-8 string routines 
  14. package Allegro.UTF8 is 
  15.  
  16.     -- UTF-8 string types 
  17.  
  18.     type Allegro_Ustr is limited 
  19.         record 
  20.             mlen : Integer; 
  21.             slen : Integer; 
  22.             data : C.Strings.chars_ptr; 
  23.         end record; 
  24.     pragma Convention( C, Allegro_Ustr ); 
  25.     type A_Allegro_Ustr is access all Allegro_Ustr; 
  26.  
  27.     type Allegro_Ustr_Info is 
  28.         record 
  29.             mlen : Integer; 
  30.             slen : Integer; 
  31.             data : C.Strings.chars_ptr; 
  32.         end record; 
  33.     type A_Allegro_Ustr_Info is access all Allegro_Ustr_Info; 
  34.  
  35.     -- Creating and destroying strings 
  36.  
  37.     function Al_Ustr_New( s : String ) return A_Allegro_Ustr; 
  38.  
  39.     function Al_Ustr_New_From_Buffer( s : String ) return A_Allegro_Ustr; 
  40.  
  41.     procedure Al_Ustr_Free( us : in out A_Allegro_Ustr ); 
  42.  
  43.     function Al_Cstr( us : A_Allegro_Ustr ) return String; 
  44.  
  45.     procedure Al_Ustr_To_Buffer( us     : A_Allegro_Ustr; 
  46.                                  buffer : out String ); 
  47.  
  48.     function Al_Cstr_Dup( us : A_Allegro_Ustr ) return String; 
  49.  
  50.     function Al_Ustr_Dup( us : A_Allegro_Ustr ) return A_Allegro_Ustr; 
  51.     pragma Import( C, Al_Ustr_Dup, "al_ustr_dup" ); 
  52.  
  53.     function Al_Ustr_Dup_Substr( us        : A_Allegro_Ustr; 
  54.                                  start_pos, 
  55.                                  end_pos   : Integer ) return A_Allegro_Ustr; 
  56.     pragma Import( C, Al_Ustr_Dup_Substr, "al_ustr_dup_substr" ); 
  57.  
  58.     -- Predefined strings 
  59.  
  60.     function Al_Ustr_Empty_String return A_Allegro_Ustr; 
  61.     pragma Import( C, Al_Ustr_Empty_String, "al_ustr_empty_string" ); 
  62.  
  63.     -- Creating strings by referencing other data 
  64.  
  65.     function Al_Ref_Cstr( info : access Allegro_Ustr_Info; s : String ) return A_Allegro_Ustr; 
  66.  
  67.     function Al_Ref_Buffer( info : access Allegro_Ustr_Info; 
  68.                             s    : String ) return A_Allegro_Ustr; 
  69.  
  70.     function Al_Ref_Ustr( info      : access Allegro_Ustr_Info; 
  71.                           us        : A_Allegro_Ustr; 
  72.                           start_pos, 
  73.                           end_pos   : Integer ) return A_Allegro_Ustr; 
  74.     pragma Import( C, Al_Ref_Ustr, "al_ref_ustr" ); 
  75.  
  76.     -- Sizes and offsets 
  77.  
  78.     function Al_Ustr_Size( us : A_Allegro_Ustr ) return C.size_t; 
  79.     pragma Import( C, Al_Ustr_Size, "al_ustr_size" ); 
  80.  
  81.     function Al_Ustr_Length( us : A_Allegro_Ustr ) return C.size_t; 
  82.     pragma Import( C, Al_Ustr_Length, "al_ustr_length" ); 
  83.  
  84.     function Al_Ustr_Offset( us : A_Allegro_Ustr; index : Integer ) return Integer; 
  85.     pragma Import( C, Al_Ustr_Offset, "al_ustr_offset" ); 
  86.  
  87.     function Al_Ustr_Next( us : A_Allegro_Ustr; pos : access Integer ) return Boolean; 
  88.  
  89.     function Al_Ustr_Prev( us : A_Allegro_Ustr; pos : access Integer ) return Boolean; 
  90.  
  91.     -- Getting code points 
  92.  
  93.     function Al_Ustr_Get( us : A_Allegro_Ustr; pos : Integer ) return Integer_32; 
  94.     pragma Import( C, Al_Ustr_Get, "al_ustr_get" ); 
  95.  
  96.     function Al_Ustr_Get_Next( us : A_Allegro_Ustr; pos : access Integer ) return Integer_32; 
  97.     pragma Import( C, Al_Ustr_Get_Next, "al_ustr_get_next" ); 
  98.  
  99.     function Al_Ustr_Prev_Get( us : A_Allegro_Ustr; pos : access Integer ) return Integer_32; 
  100.     pragma Import( C, Al_Ustr_Prev_Get, "al_ustr_prev_get" ); 
  101.  
  102.     -- Inserting into strings 
  103.  
  104.     function Al_Ustr_Insert( us1 : A_Allegro_Ustr; 
  105.                              pos : Integer; 
  106.                              us2 : A_Allegro_Ustr ) return Boolean; 
  107.  
  108.     function Al_Ustr_Insert_Cstr( us  : A_Allegro_Ustr; 
  109.                                   pos : Integer; 
  110.                                   us2 : String ) return Boolean; 
  111.  
  112.     function Al_Ustr_Insert_Chr( us  : A_Allegro_Ustr; 
  113.                                  pos : Integer; 
  114.                                  chr : Integer_32 ) return C.size_t; 
  115.     pragma Import( C, Al_Ustr_Insert_Chr, "al_ustr_insert_chr" ); 
  116.  
  117.     -- Appending to strings 
  118.  
  119.     function Al_Ustr_Append( us1, us2 : A_Allegro_Ustr ) return Boolean; 
  120.  
  121.     function Al_Ustr_Append_Cstr( us : A_Allegro_Ustr; s : String ) return Boolean; 
  122.  
  123.     function Al_Ustr_Append_Chr( us : A_Allegro_Ustr; chr : Integer_32 ) return C.size_t; 
  124.     pragma Import( C, Al_Ustr_Append_Chr, "al_ustr_append_chr" ); 
  125.  
  126.     -- Removing parts of strings 
  127.  
  128.     function Al_Ustr_Remove_Chr( us : A_Allegro_Ustr; pos : Integer ) return Boolean; 
  129.  
  130.     function Al_Ustr_Remove_Range( us        : A_Allegro_Ustr; 
  131.                                    start_pos, 
  132.                                    end_pos   : Integer ) return Boolean; 
  133.  
  134.     function Al_Ustr_Truncate( us : A_Allegro_Ustr; start_pos : Integer ) return Boolean; 
  135.  
  136.     function Al_Ustr_Ltrim_Ws( us : A_Allegro_Ustr ) return Boolean; 
  137.  
  138.     function Al_Ustr_Rtrim_Ws( us : A_Allegro_Ustr ) return Boolean; 
  139.  
  140.     function Al_Ustr_Trim_Ws( us : A_Allegro_Ustr ) return Boolean; 
  141.  
  142.     -- Assigning one string to another 
  143.  
  144.     function Al_Ustr_Assign( us1, us2 : A_Allegro_Ustr ) return Boolean; 
  145.  
  146.     function Al_Ustr_Assign_Substr( us1, us2  : A_Allegro_Ustr; 
  147.                                     start_pos, 
  148.                                     end_pos   : Integer ) return Boolean; 
  149.  
  150.     function Al_Ustr_Assign_Cstr( us1 : A_Allegro_Ustr; s : String ) return Boolean; 
  151.  
  152.     -- Replacing parts of a string 
  153.  
  154.     function Al_Ustr_Set_Chr( us  : A_Allegro_Ustr; 
  155.                               pos : Integer; 
  156.                               chr : Integer_32 ) return C.size_t; 
  157.     pragma Import( C, Al_Ustr_Set_Chr, "al_ustr_set_chr" ); 
  158.  
  159.     function Al_Ustr_Replace_Range( us1        : A_Allegro_Ustr; 
  160.                                     start_pos1, 
  161.                                     end_pos1   : Integer; 
  162.                                     us2        : A_Allegro_Ustr ) return Boolean; 
  163.  
  164.     -- Searching 
  165.  
  166.     function Al_Ustr_Find_Chr( us        : A_Allegro_Ustr; 
  167.                                start_pos : Integer; 
  168.                                c         : Integer_32 ) return Integer; 
  169.     pragma Import( C, Al_Ustr_Find_Chr, "al_ustr_find_chr" ); 
  170.  
  171.     function Al_Ustr_Rfind_Chr( us        : A_Allegro_Ustr; 
  172.                                 start_pos : Integer; 
  173.                                 c         : Integer_32 ) return Integer; 
  174.     pragma Import( C, Al_Ustr_Rfind_Chr, "al_ustr_rfind_chr" ); 
  175.  
  176.     function Al_Ustr_Find_Set( us         : A_Allegro_Ustr; 
  177.                                start_pos  : Integer; 
  178.                                acceptChrs : A_Allegro_Ustr ) return Integer; 
  179.     pragma Import( C, Al_Ustr_Find_Set, "al_ustr_find_set" ); 
  180.  
  181.     function Al_Ustr_Find_Set_Cstr( us         : A_Allegro_Ustr; 
  182.                                     start_pos  : Integer; 
  183.                                     acceptChrs : String ) return Integer; 
  184.  
  185.     function Al_Ustr_Find_Cset( us        : A_Allegro_Ustr; 
  186.                                 start_pos : Integer; 
  187.                                 reject    : A_Allegro_Ustr ) return Integer; 
  188.     pragma Import( C, Al_Ustr_Find_Cset, "al_ustr_find_cset" ); 
  189.  
  190.     function Al_Ustr_Find_Cset_Cstr( us        : A_Allegro_Ustr; 
  191.                                      start_pos : Integer; 
  192.                                      reject    : String ) return Integer; 
  193.  
  194.     function Al_Ustr_Find_Str( haystack  : A_Allegro_Ustr; 
  195.                                start_pos : Integer; 
  196.                                needle    : A_Allegro_Ustr ) return Integer; 
  197.     pragma Import( C, Al_Ustr_Find_Str, "al_ustr_find_str" ); 
  198.  
  199.     function Al_Ustr_Find_Cstr( haystack  : A_Allegro_Ustr; 
  200.                                 start_pos : Integer; 
  201.                                 needle    : String ) return Integer; 
  202.  
  203.     function Al_Ustr_Rfind_Str( haystack  : A_Allegro_Ustr; 
  204.                                 start_pos : Integer; 
  205.                                 needle    : A_Allegro_Ustr ) return Integer; 
  206.     pragma Import( C, Al_Ustr_Rfind_Str, "al_ustr_rfind_str" ); 
  207.  
  208.     function Al_Ustr_Rfind_Cstr( haystack  : A_Allegro_Ustr; 
  209.                                  start_pos : Integer; 
  210.                                  needle    : String ) return Integer; 
  211.  
  212.     function Al_Ustr_Find_Replace( us        : A_Allegro_Ustr; 
  213.                                    start_pos : Integer; 
  214.                                    find      : A_Allegro_Ustr; 
  215.                                    replace   : A_Allegro_Ustr ) return Boolean; 
  216.  
  217.     function Al_Ustr_Find_Replace_Cstr( us        : A_Allegro_Ustr; 
  218.                                         start_pos : Integer; 
  219.                                         find      : String; 
  220.                                         replace   : String ) return Boolean; 
  221.  
  222.     -- Comparing 
  223.  
  224.     function Al_Ustr_Equal( us1, us2 : A_Allegro_Ustr ) return Boolean; 
  225.  
  226.     function Al_Ustr_Compare( u, v : A_Allegro_Ustr ) return Integer; 
  227.     pragma Import( C, Al_Ustr_Compare, "al_ustr_compare" ); 
  228.  
  229.     function Al_Ustr_Ncompare( us1, us2 : A_Allegro_Ustr; n : Integer ) return Integer; 
  230.     pragma Import( C, Al_Ustr_Ncompare, "al_ustr_ncompare" ); 
  231.  
  232.     function Al_Ustr_Has_Prefix( u, v : A_Allegro_Ustr ) return Boolean; 
  233.  
  234.     function Al_Ustr_Has_Prefix_Cstr( u : A_Allegro_Ustr; s : String ) return Boolean; 
  235.  
  236.     function Al_Ustr_Has_Suffix( u, v : A_Allegro_Ustr ) return Boolean; 
  237.  
  238.     function Al_Ustr_Has_Suffix_Cstr( us1 : A_Allegro_Ustr; s : String ) return Boolean; 
  239.  
  240.     -- UTF-16 conversion 
  241.  
  242.     function Al_Ustr_New_From_Utf16( s : Wide_String ) return A_Allegro_Ustr; 
  243.  
  244.     function Al_Ustr_Size_Utf16( us : A_Allegro_Ustr ) return C.size_t; 
  245.     pragma Import( C, Al_Ustr_Size_Utf16, "al_ustr_size_utf16" ); 
  246.  
  247.     -- 'size' is in bytes, not characters! 
  248.     procedure Al_Ustr_Encode_Utf16( us   : A_Allegro_Ustr; 
  249.                                     s    : out Wide_String; 
  250.                                     size : out C.size_t ); 
  251.  
  252.     -- Low-level UTF-8 routines 
  253.  
  254.     function Al_Utf8_Width( chr : Integer_32 ) return C.size_t; 
  255.     pragma Import( C, Al_Utf8_Width, "al_utf8_width" ); 
  256.  
  257.     procedure Al_Utf8_Encode( chr  : Integer_32; 
  258.                               s    : out String; 
  259.                               size : out C.size_t ); 
  260.     pragma Precondition( s'Length = 4 ); 
  261.  
  262.     -- Low-level UTF-16 routines 
  263.  
  264.     function C_Al_Utf16_Width( chr : Integer_32 ) return C.size_t; 
  265.     pragma Import( C, C_Al_Utf16_Width, "al_utf16_width" ); 
  266.  
  267.     procedure Al_Utf16_Encode( chr  : Integer_32; 
  268.                                s    : out Wide_String; 
  269.                                size : out C.size_t ); 
  270.     pragma Precondition( s'Length = 2 ); 
  271.  
  272. end Allegro.UTF8;