string.anubis 20.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948

 *Project*                             The Anubis Project
   
 *Title*                                 String Toolbox
   
 *Copyright*                     Copyright (c) David René 2006-2007. 
 
 *Created: 2006 04 05
 *Author*  David René
 *Status*  Released

 *Overview* 
 
 This file contain some helper functions for string manipulation.
 At the beginning that toolbox is the result of refactoring and gathering of
 very useful functions used for string manipulation.
 The idea here, is to let everybody use these functions which facilitate the life
 of Anubis programmer.
 If you made new functions, and you think it will be good for Anubis community,
 you can make a proposal of integration of your code in Anubis Standard Toolbox
 be sending mail at toolbox@anubis-language.com
 
 *Description*
   return the string with 0 add at begining of given number. The width mean the 
   number of digit in total, including the given number convert into string.
 
     for example if you call zero_pad_n(3, 25) the return string is "025" 
 
   because that function is ZERO padding function, it is not in responsable to 
   cut the converted number to the right given width size. 
   If you call zero_pad_n(2,234) the return value will be 234.

read tools/basis.anubis
read tools/findstring.anubis


   
   'crlf'  is a  string which  is required  by  the HTTP  protocol to  mark the  end of  a
   line. Any HTTP request or HTTP answer has  two parts (the second one may be empty): the
   header and the body. The header is  made of several lines (at least one), and separated
   from the body by a blank line. In other words, the header is separated from the body by
   a ''double crlf''.

public define String cr       = "\r".       //implode([(Word8)13]).
public define String lf       = "\n".       //implode([(Word8)10]).  
public define String crlf     = "\r\n".     //implode([(Word8)13,10]).     i.e ASCII characters:   13 10  (in this order)
public define String crlfcrlf = "\r\n\r\n". //implode([(Word8)13,10,13,10]).   


  
   This file defines the function 'find':
   
public define Maybe(Int)
  find
    (
      String t,         // find 't' in 's'
      String s,
      Int n
    ). 
   
   which returns:
   
     - 'success(m)' if the first occurence of 't' in 's' after position 'n' is at position
   'm',
   
     - 'failure' otherwise.

   
   The same one for byte arrays: 
   
public define Maybe(Int)
  find
    (
      ByteArray t,         // find 't' in 's'
      ByteArray s,
      Int n
    ). 
   
  return true if the string 'a' start with the string 'b'
  
public define Bool
  start_with
  (
    String  a,
    String  b
  ).
   
   It also defines 'find_all':
   
public define List(Int)       // list of all positions found
  find_all
    (
      String t,    // search for t
      String s     // within s
    ).
   
   which returns the list of all the positions at which 't' may be found in 's'. 
   
   Note:  (by exception)  if  't' is  empty,  these functions  return  'failure' and  '[]'
   respectively.
   
   
   --- That's all for the public part ! --------------------------------------------------

   
   
   
   

   The  following function  returns 'true'  if 't'  minus the  'p' first  characters  is a
   substring of 's' at position 'n', 'false' otherwise.
   
define Bool
  is_substring_at
    (
      String t,
      Int p, 
      String s,
      Int n
    ) = 
  if nth(p,t) is
    {
      failure then true,              // position 'p' outside 't'
      success(c) then 
        if nth(n,s) is 
          {
            failure then false,       // position 'n' outside 's'
            success(d) then 
              if c = d
              then is_substring_at(t,p+1,s,n+1)    // terminal call
              else false
          }
    }.
   
   
define Bool
   is_sub_byte_array_at
     (
       ByteArray t,
       Int p, 
       ByteArray s,
       Int n
     ) =
   if nth(p,t) is 
     {
       failure then true,
       success(c) then 
         if nth(n,s) is 
           {
             failure then false, 
             success(d) then 
               if c = d
               then is_sub_byte_array_at(t,p+1,s,n+1) 
               else false
           }
     }.
   
   
   The following function returns 'failure' if 't'  is not a substring of 's', starting at
   position  'n'  in  's', and  the  position  of  the  first  occurence otherwise  (in  a
   'success'). Here 'c' is assumed to be the first character of 't'. 
   
define Maybe(Int)
  find
    (
      Int n, 
      Word8 c, 
      String t,
      String s
    ) =
  if nth(n,s) is
    {
      failure then failure, 
      success(d) then 
        if c = d
        then if is_substring_at(t,0,s,n) 
             then success(n)
             else find(n+1,c,t,s)
        else find(n+1,c,t,s)
    }. 
   
define Maybe(Int)
  find
    (
      Int n,            // start of search within s
      Word8 c,             // first byte of t
      ByteArray t,
      ByteArray s
    ) =
  if nth(n,s) is
    {
      failure then failure, 
      success(d) then 
        if c = d
        then if is_sub_byte_array_at(t,0,s,n) 
             then success(n)
             else find(n+1,c,t,s)
        else find(n+1,c,t,s)
    }. 
   
   

   Now, here are our functions. 
   
public define Maybe(Int)
  find
    (
      String t, 
      String s,
      Int n
    ) =
  if nth((Int)0,t) is 
    {
      failure then success(n), 
      success(c) then find(n,c,t,s)
    }. 
   
public define Maybe(Int)
  find
    (
      ByteArray t, 
      ByteArray s,
      Int n
    ) =
   //print("   find("+to_string(t)+",_,"+integer_to_string(n)+")\n"); 
  if nth(0,t) is 
    {
      failure then success(n), 
      success(c) then find(n,c,t,s)
    }. 
   
define List(Int)
  find_all
    (
      String t,
      String s,
      Int n,
      List(Int) so_far
    ) =
  if find(t,s,n) is
    {
      failure then reverse(so_far),
      success(m) then find_all(t,s,m+1,[m . so_far])
    }. 
  
public define List(Int)
  find_all
     (
       String t, 
       String s
     ) =
  find_all(t,s,0,[]). 
   

public define Bool
  start_with
  (
    String  a,
    String  pattern
  )=
  with pattern_len = length(pattern),
  if sub_string(a, 0, pattern_len) is
  {
    failure         then false,
    success(start)  then 
      if start = pattern then 
        true
      else
        false
  }.
   
   
   --------------------------------------------------------------------------------
   

  
define String 
  zero_pad_n
  ( 
    Int  width, 
    String  number 
  ) =
  if length(number) < width then
    zero_pad_n(width, "0"+number)
  else 
    number.

public define Word8
  to_lower
    (
      Word8 c
    ) =
  if ('A' +=< c & c +=< 'Z')
  then c - 'A' + 'a'
  else c. 
   
public define Word8
  to_upper
    (
      Word8 c
    ) =
  if ('a' +=< c & c +=< 'z')
  then c - 'a' + 'A'
  else c. 

   Putting a string to lower case. 
   
define String 
  convert_string   
    (
      String s,
      Int n, 
      List(Word8) so_far,
      (Word8) -> Word8 convert_fn
    ) =
  if nth(n,s) is 
    {
      failure then implode(reverse(so_far)), 
      success(c) then
        convert_string(s, n+1, [convert_fn(c) . so_far], convert_fn)
    }.
   
public define String
  to_lower
    (
      String s
    ) =
  convert_string(s, 0, [], to_lower). 

public define String
  to_upper
    (
      String s
    ) =
  convert_string(s, 0, [], to_upper). 
  
public define String 
  zero_pad_n
  ( 
    Int width, 
    Int number 
  ) =
  with s = to_decimal(number),
  if length(s) < width then
    zero_pad_n(width, "0"+s)
  else 
    s.

  /**
   * fill_or_cut test the length of given string (src_str) with the size argument. 
   * If the length is smaller than size the function return a new string, composed with
   * src_str padded with the given ch.
   * If the length is greater than size, the return string if a part of src_str cut at size
   *  example:  
   *  src_str = "gabuzomeu", size = 10, ch = " " (space)
   *  Because the length of gabuzomeu is 9, the function add 1 space at is right side. 
   *  The return string will be = "gabuzomeu ".
   * 
   *  src_str = "gabuzomeu", size = 4, ch = " " (space)
   *  Because the length of gabuzomeu is 9, the function cut the string at is character 4. 
   *  The return string will be = "gabu".
   **/

public define String
  fill_or_cut
  (
    String  src_str,
    Int   size,
    Word8   ch
  )=
  with str_size = length(src_str),
  if str_size = size then
    src_str
  else if str_size > size then
    with ba_str = to_byte_array(src_str),
         truncate(ba_str, size);
         to_string(ba_str)
  else
    src_str + to_string(constant_byte_array(size - str_size, ch)).
  
public define Bool
  is_white_char
  (
    Word8 ch
  ) =
  ch = 32 | ch = 9 | ch = 10 | ch = 13.
  
define (List(Word8), List(Word8))
  in_word
  (
    List(Word8) so_far,
    List(Word8) current
  ) =
  if current is
  {
    []      then (so_far, []),
    [h . t] then
    if is_white_char(h) then
      (so_far, t)
    else
      in_word([h . so_far], t)
  }.
  
define Maybe(List(Word8))
  search_first_char
  (
    List(Word8) current
  ) =
  if current is
  {
    []        then failure,
    [h . t]   then
    if is_white_char(h) then //we skip 32 ASCII code (space) and 9 ASCII code (tabulation)
      search_first_char(t)
    else
      if in_word([h], t) is (word, _) then success(word)
   }.
     
public define Maybe(String)
  first_word
  (
    String input
  ) =
  if search_first_char(explode(input)) is
  {
    failure     then failure,
    success(l)  then success(implode(reverse(l)))
  }.
  

public define Maybe(String)
  first_word
  (
    String  input, 
    Int   start
  ) =

  if sub_string(input, start, length(input) - start) is
  {
    failure     then failure,
    success(s)  then first_word(s)
  }.

define Maybe(List(String))
  list_word
  (
    List(Word8)    input,
    List(String)  string_so_far
  ) =
  if input is
  {
    []  then //we have touch the end of int8 list
      if string_so_far is 
      { 
        []    then failure, //if the list of string is empty we have failed to find out words 
        [_ . _] then success(reverse(string_so_far))
      },
    [ h . t ] then
      if is_white_char(h) then 
        list_word(t, string_so_far)
      else
        if in_word([h], t) is (word, so_far) then
          list_word(so_far, [implode(reverse(word)). string_so_far])
  }.
  
public define Maybe(List(String))
  list_word
  (
    String  input,
    Int   start
  )=
  if sub_string(input, start, length(input) - start) is
  {
    failure     then failure,
    success(s)  then list_word(explode(s),[])
  }.

public define Maybe(List(String))
  list_word
  (
    String  input,
  )=
  list_word(input, 0).

define List(String)
  to_string_list
  (
    ByteArray ba,
    List(String)  so_far,
    Int         last_found_idx,
    Int         current_idx,
    Int         end_idx
  )=
  if current_idx > end_idx then  //its finish  
    reverse(so_far)               //and return the found list in right way
  else if force_nth(current_idx, ba) = 0 then
    with str = to_string(extract(ba, last_found_idx, current_idx)),
    to_string_list(ba, [ str . so_far], current_idx +1, current_idx +1, end_idx)
  else
    to_string_list(ba, so_far, last_found_idx, current_idx+1, end_idx)
  .
  
/** Transform a ByteArray into list of String. That ByteArray contain
 *  any number of C null terminated string, such as "toto\0tata\0". the
 *  result string list with that ByteArray is a list with "toto", "tata".
 *  The ByteArray can start with \0, this mean the first string is empty.
 *  @param ba_org The ByteArray which must be transform
 *  @return list of string(s) found
 */
public define List(String)
  to_string_list
  (
    ByteArray ba_org
  )=
  with len = length(ba_org),  
  to_string_list(ba_org, [], 0, 0, len).
  
/**
 * convert the content of Word32 into string
 * for example if the content of that integer is
 * readable ascii characters, there are converted
 * into 8 bits integer and finally printable ASCII String
 * 
 * the Word32 _B_STRING_TYPE  = 1129534546.  defined in muscle.anubis 
 * can be print by to_ascii like that 'CSTR'
 * 
 * @param content the 32bits integer to convert
 * @return the string with the 4 ascii characters
 */
 
public define String
  to_ascii
  (
    Word32 content
  ) =
  with ba = constant_byte_array(4, 0),
  if content is word32(l,h) then 
  if l is word16(b1,b2) then
  if h is word16(b3,b4) then 
  forget(put(ba, 0, b4));
  forget(put(ba, 1, b3));
  forget(put(ba, 2, b2));
  forget(put(ba, 3, b1));
  to_string(ba).


define String
  get_hexa_digit
  (
    Word8  digit
  ) =
  if digit +=< 9 
  then implode([digit+48])
  else if digit +=< 16
       then implode([digit+55])
       else "?". 
   
  How it was before version 1.8:
  with the_digit = word8_to_Word32(digit),
  if the_digit =< 9 then
    implode([truncate_to_word8(the_digit + 48)])
  else if the_digit =< 16 then
    implode([truncate_to_word8(the_digit + 55)])
  else
    "?".

   
   
 public define String  // already defined in predefined.anubis
  to_hexa
  (
    Word8  value
  ) =
  with val = word8_to_Word32(value),
  get_hexa_digit(truncate_to_word8((val & 0xF0) >> 4)) + get_hexa_digit(truncate_to_word8(val & 0xF)).

define String 
  get_hexa_string
  (
    ByteArray src,
    Int     idx,
    Int     left,
    String    so_far
  )=
  if left = 0 then
    so_far
  else
    get_hexa_string(src, idx + 1, left -1, so_far + to_hexa(force_nth(idx, src))).
  
 public define String
  to_hexa
  (
    ByteArray src
  )=
  get_hexa_string(src, 0, length(src),"").
  
 public define String  (already defined in predefined.anubis)
  to_hexa
  (
    Word32 src
  )=
  to_hexa(truncate_to_word8(src >> 24))          +
  to_hexa(truncate_to_word8((src >> 16) & 0xFF)) +
  to_hexa(truncate_to_word8((src >> 8) & 0xFF))  +
  to_hexa(truncate_to_word8(src & 0xFF)).
  
/** extract the domain name from email address. 
 *  for webmaster@anubis-language.com that function will return
 *  "anubis-language.com" 
 */ 
public define Maybe(String)
  extract_domain_name
  (
    String  email
  )=
  if find("@", email, 0) is
  {
    failure           then failure,
    success(position) then sub_string(email, position+1, length(email) - (position+1))
  }.


define List(String)
  split_by_tokens
  (
    List(Word8)   line,
    List(Word8)   tokens,
    List(Word8)   current,
    List(String)  so_far
  ) =
  if line is
  {
    []  then 
      if length(current) > 0 then
        reverse([ implode(reverse(current)) . so_far])
      else
        reverse(so_far),
    [ char . t ] then
      if member(tokens, char) then
        split_by_tokens( t, tokens, [], [ implode(reverse(current)) . so_far])        
      else
        split_by_tokens( t, tokens, [ char . current ], so_far)
  }.

public define List(String)
  split_by_tokens
  (
    String      line,
    List(Word8) tokens
  )=
  split_by_tokens(explode(line), tokens, [], []).
  
public define List(String)
  split_by_token
  (
    String  line,
    Word8   token
  )=
  split_by_tokens(explode(line), [token], [], []).

public define List(String)
  split
  (
    String  line,
    Word8   token
  )=
  split_by_tokens(explode(line), [token], [], []).
   
define List(String)
  _split_lines
  (
    List(Word8)   text,
    List(Word8)   current,
    List(String)  so_far
  ) =
  if text is
  {
    []  then 
      if length(current) > 0 then
        reverse([ implode(reverse(current)) . so_far])
      else
        reverse(so_far),
    [ char . t ] then
      if char = 13 then // CR
        if t is
        {
          [] then
            if length(current) > 0 then
              reverse([ implode(reverse(current)) . so_far])
            else
              reverse(so_far),
          [ char2 . t2 ] then
            if char2 = 10 then // LF
              _split_lines( t2, [], [ implode(reverse(current)) . so_far])        
            else
              _split_lines( t, [], [ implode(reverse(current)) . so_far])        
        }
      else if char = 10 then // LF
        _split_lines( t, [], [ implode(reverse(current)) . so_far])        
      else
        _split_lines( t, [ char . current ], so_far)
  }.

/// Split a text into lines, whether the line ends are (CRLF / LF / CR)
public define List(String)
  split_lines
  (
    String  text,
  )=
  _split_lines(explode(text), [], []).
   
define (Int, Int)
  trim
  (
    List(Word8)  left,
    Word8 -> Bool is_token,
    Int       start,
    Int       last_char,
    Int       current
  )=
  if left is
  {
    []      then (start, last_char),
    [h . t] then
      if is_token(h) then
        trim(t, is_token, start, last_char, current + 1)
      else
        trim(t, is_token, start, current + 1, current + 1)
  }.
  
define (Int, Int)
  trim
  (
    List(Word8)   string,
    Word8 -> Bool is_token,
    Int         current
  )=
  if string is
  {
    []        then (0, 0),
    [ h . t ] then
      if is_token(h) then
        trim(t, is_token, current + 1)
      else        
        trim(t, is_token, current, 1, 1)
  }.
    
public define String
  trim
  (
    String string
  )=
  if trim(explode(string), is_white_char, 0) is (start, length) then
    if sub_string(string, start, length) is
    {
      failure     then "",
      success(s)  then s
    }.
 
public define String
  trim_token
  (
    String string,
    Word8   token
  )=
  with is_token_fn = (Word8 c) |-> c = token,
  if trim(explode(string), is_token_fn, 0) is (start, length) then
    if sub_string(string, start, length) is
    {
      failure     then "",
      success(s)  then s
    }.

public define String
  trim_tokens
  (
    String      string,
    List(Word8) tokens
  )=
  with is_token_fn = (Word8 c) |-> mapor((Word8 token) |-> c = token, tokens),
  if trim(explode(string), is_token_fn, 0) is (start, length) then
    if sub_string(string, start, length) is
    {
      failure     then string,
      success(s)  then s
    }.
    
public define String
   append
     (
       List(String) l
     ) =
   if l is 
     {
       [ ]      then "", 
       [h . t]  then h+append(t)
     }.
   
   
public define String
   append
     (
       NonEmptyList(String) l
     ) =
   if l is 
     {
       [h . t]  then h+append(t)
     }.

define String
  join
  (
    String sep,
    List(String) l,
    String result
  ) =
   if l is 
     {
       [ ]      then result, 
       [h . t]  then join(sep, t, result + sep + h)
     }.


public define String
  join
  (
    String sep,
    List(String) l
  ) =
   if l is 
     {
       [ ]      then "", 
       [h . t]  then join(sep, t, h)
     }.

  // *** COMPARISON

define Bool
  insensitive_equal
  (
    List(Word8)  l1,
    List(Word8)  l2,
    Int       index
  )=
  if to_lower(force_nth(index, l1, 0)) = to_lower(force_nth(index, l2, 0)) then
    if index = 0 then
      true
    else
      insensitive_equal(l1, l2, index-1)
  else
    false.

public define Bool
  insensitive_equal
  (
    String str1,
    String str2
  )=
  if length(str1) = length(str2) then
    if length(str1) = 0 then
      true
    else
      insensitive_equal(explode(str1), explode(str2), length(str1)-1)
  else
    false
  .

define List(Word8)
  _find_and_replace
  (
    List(Word8) original,
    List(Word8) new_string,
    List(Word8) looking_for,
    List(Word8) replace_with,
    Int       pos,
    List(Word8) temp_str,
  ) =
  if original is
  {
    []      then new_string,
    [h . t] then 
      if nth(pos, looking_for) is
      {
        failure then _find_and_replace(t, [h . (temp_str + new_string)], looking_for, replace_with, 0, []),
        success(c) then
          if h = c then 
            with temp_str2 = (List(Word8))[h . temp_str],
            if length(looking_for) = length(temp_str2) then
              _find_and_replace(t, replace_with + new_string, looking_for, replace_with, 0, [])
            else
              _find_and_replace(t, new_string, looking_for, replace_with, pos + 1, [h . temp_str])
          else 
            if implode(looking_for) = implode(reverse(temp_str)) then
              _find_and_replace(t, [h . (replace_with + new_string)], looking_for, replace_with, 0, [])
            else
              _find_and_replace(t, [h . (temp_str + new_string)], looking_for, replace_with, 0, [])
      }
  }.

public define String
  find_and_replace
  (
    String original,
    String looking_for,
    String replace_with
  ) =
  implode(reverse(_find_and_replace(explode(original), [], explode(looking_for), reverse(explode(replace_with)), 0, []))).


define String
  repeat_string
  (
    List(Word8) s,
    Int         i,
    List(Word8) so_far
  ) =
  if i > 0 then
    repeat_string(s, i - 1, so_far + s)
  else
    implode(so_far).


public define String
   String s * Int i =
      repeat_string(explode(s), i, []).
















 global define One
  test_find_and_replace
  (
    List(String) argv
  ) =
  print(find_and_replace(force_nth(0, argv), force_nth(1, argv), force_nth(2, argv)));
  unique.