Seed7 
 FAQ 
 Manual 
 Screenshots 
 Examples 
 Algorithms 
 Download 
 Links 

 Algorithms 
 Sorting 
 Searching 
 Date & Time 
 String 
 Mathematics 
 File 
 Puzzles 
 Others 
 Algorithms 
String
 previous   up   next 

Replace tabs with the corresponding number of spaces

const proc: delTabs (inout string: stri, in integer: tabCount) is func
  local
    const integer: tabJump is 8;
    var integer: tabPos is 0;
    var integer: blankCount is 0;
  begin
    tabPos := pos(stri, "\t");
    while tabPos <> 0 do
      blankCount := tabJump - pred(tabPos) rem tabJump;
      stri := stri[ .. pred(tabPos)] &
          " " mult blankCount & stri[succ(tabPos) .. ];
      tabPos := pos(stri, "\t");
    end while;
  end func;

Replace leading spaces with the corresponding number of tabs

const proc: insertLeadingTabs (inout string: stri, in integer: tabCount) is func
  local
    var integer: blankCount is 0;
    var integer: tabCount is 0;
    var integer: blanksRemaining is 0;
    var integer: charssaved is 0;
  begin
    if length(stri) <> 0 then
      if stri[1] = ' ' then
        blankCount := 2;
        while stri[blankCount] = ' ' do
          incr(blankCount);
        end while;
        decr(blankCount);
        if blankCount >= tabJump then
          tabCount := blankCount div tabJump;
          blanksRemaining := blankCount rem tabJump;
          charssaved := blankCount - (tabCount + blanksRemaining);
          stri := "\t" mult tabCount & stri[succ(charssaved + tabCount) .. ];
        end if;
      end if;
    end if;
  end func;

Encode a string with the Base64 encoding

This function is part of the "encoding.s7i" library.

const func string: toBase64 (in string: stri) is func
  result
    var string: result is "";
  local
    const string: coding is "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var integer: index is 1;
    var integer: subIndex is 1;
    var char: ch is ' ';
    var integer: threeBytes is 0;
    var string: fourBytes is "    ";
    var integer: posToAddNewline is 58;
  begin
    for index range 1 to length(stri) step 3 do
      threeBytes := 0;
      for subIndex range index to index + 2 do
        if subIndex <= length(stri) then
          ch := stri[subIndex];
          if ch >= '\256\' then
            raise RANGE_ERROR;
          end if;
        else
          ch := '\0\';
        end if;
        threeBytes := (threeBytes << 8) + ord(ch);
      end for;
      fourBytes @:= [1] coding[succ( threeBytes >> 18)];
      fourBytes @:= [2] coding[succ((threeBytes >> 12) mod 64)];
      fourBytes @:= [3] coding[succ((threeBytes >>  6) mod 64)];
      fourBytes @:= [4] coding[succ( threeBytes        mod 64)];
      if index = posToAddNewline then
        result &:= "\n";
        posToAddNewline +:= 57;
      end if;
      result &:= fourBytes;
    end for;
    index := length(result);
    if length(stri) rem 3 = 2 then
      result @:= [index] '=';
    elsif length(stri) rem 3 = 1 then
      result @:= [index] '=';
      result @:= [pred(index)] '=';
    end if;
  end func;

Decode a Base64 encoded string

This function is part of the "encoding.s7i" library.

const func string: fromBase64 (in string: stri) is func
  result
    var string: result is "";
  local
    const array integer: decode is [] (                      # -1 is illegal
        62, -1, -1, -1, 63,                                  # + /
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61,              # 0 - 9
        -1, -1, -1,  0, -1, -1, -1,                          # =
         0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12,  # A - M
        13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,  # N - Z
        -1, -1, -1, -1, -1, -1,
        26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,  # a - m
        39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51); # n - z
    var integer: index is 1;
    var integer: subIndex is 1;
    var integer: number is 0;
    var integer: fourBytes is 0;
    var string: threeBytes is "   ";
  begin
    while index <= length(stri) - 3 do
      if stri[index] >= '+' then
        fourBytes := 0;
        for subIndex range index to index + 3 do
          number := decode[ord(stri[subIndex]) - ord(pred('+'))];
          if number = -1 then
            raise RANGE_ERROR;
          end if;
          fourBytes := (fourBytes << 6) + number;
        end for;
        threeBytes @:= [1] chr( fourBytes >> 16);
        threeBytes @:= [2] chr((fourBytes >>  8) mod 256);
        threeBytes @:= [3] chr( fourBytes        mod 256);
        result &:= threeBytes;
        index +:= 4;
      elsif stri[index] = '\n' then
        incr(index);
      elsif stri[index] = '\r' or stri[succ(index)] = '\n' then
        index +:= 2;
      else
        raise RANGE_ERROR;
      end if;
    end while;
    if index <> succ(length(stri)) then
      raise RANGE_ERROR;
    end if;
    if stri[length(stri) - 1 len 2] = "==" then
      result := result[.. length(result) - 2];
    elsif length(stri) >= 1 and stri[length(stri)] = '=' then
      result := result[.. pred(length(result))];
    end if;
  end func;

Encode a string with the Quoted-printable encoding

This function is part of the "encoding.s7i" library.

const func string: toQuotedPrintable (in string: stri) is func
  result
    var string: result is "";
  local
    var integer: index is 0;
    var integer: startPos is 1;
    var integer: counter is 1;
    var char: ch is ' ';
  begin
    for index range 1 to length(stri) do
      ch := stri[index];
      if ch >= '\256\' then
        raise RANGE_ERROR;
      elsif ch = '\n' or (ch = '\r' and
          index < length(stri) and stri[succ(index)] = '\n') then
        if index > 1 then
          ch := stri[pred(index)];
          if ch = ' ' or ch = '\t' then
            result &:= stri[startPos .. index - 2];
            if counter >= 76 then
              result &:= "=\n";
              counter := 1;
            end if;
            result &:= "=" <& str(ord(stri[pred(index)]), 16) lpad0 2;
            counter +:= 3;
            startPos := index;
          end if;
        end if;
        counter := 1;
      elsif ch >= '\127\' or ch = '=' or (ch < ' ' and ch <> '\9\') then
        result &:= stri[startPos .. pred(index)];
        if counter >= 74 then
          result &:= "=\n";
          counter := 1;
        end if;
        result &:= "=" <& str(ord(ch), 16) lpad0 2;
        startPos := succ(index);
        counter +:= 3;
      elsif counter >= 76 then
        result &:= stri[startPos .. pred(index)] & "=\n";
        startPos := index;
        counter := 2;
      else
        incr(counter);
      end if;
    end for;
    result &:= stri[startPos ..];
  end func;

Decode a Quoted-printable encoded string

This function is part of the "encoding.s7i" library.

const func string: fromQuotedPrintable (in string: stri) is func
  result
    var string: result is "";
  local
    var integer: startPos is 1;
    var integer: equalSignPos is 0;
    var string: twoChars is "";
  begin
    equalSignPos := pos(stri, "=");
    while equalSignPos <> 0 do
      result &:= stri[startPos .. pred(equalSignPos)];
      if equalSignPos < length(stri) and
          stri[succ(equalSignPos)] = '\n' then
        startPos := equalSignPos + 2;
      elsif equalSignPos <= length(stri) - 2 then
        twoChars := stri[succ(equalSignPos) len 2];
        if twoChars <> "\r\n" then
          result &:= str(chr(toInt(twoChars, 16)));
        end if;
        startPos := equalSignPos + 3;
      else
        raise RANGE_ERROR;
      end if;
      equalSignPos := pos(stri, "=", startPos);
    end while;
    result &:= stri[startPos ..];
  end func;

Uuencode a string

This function is part of the "encoding.s7i" library.

const func string: toUuencoded (in string: stri) is func
  result
    var string: result is "";
  local
    var integer: index is 1;
    var integer: subIndex is 1;
    var char: ch is ' ';
    var integer: threeBytes is 0;
    var string: fourBytes is "    ";
    var integer: posToAddNewline is 43;
  begin
    if length(stri) <> 0 then
      if length(stri) < 45 then
        result &:= str(chr(32 + length(stri)));
      else
        result &:= "M";
      end if;
      for index range 1 to length(stri) step 3 do
        threeBytes := 0;
        for subIndex range index to index + 2 do
          if subIndex <= length(stri) then
            ch := stri[subIndex];
            if ch >= '\256\' then
              raise RANGE_ERROR;
            end if;
          else
            ch := '\0\';
          end if;
          threeBytes := (threeBytes << 8) + ord(ch);
        end for;
        fourBytes @:= [1] chr(32 + (threeBytes >> 18));
        fourBytes @:= [2] chr(32 + (threeBytes >> 12) mod 64);
        fourBytes @:= [3] chr(32 + (threeBytes >>  6) mod 64);
        fourBytes @:= [4] chr(32 +  threeBytes        mod 64);
        result &:= fourBytes;
        if index = posToAddNewline then
          if length(stri) <= index + 2 then
            noop;
          elsif length(stri) - index - 2 < 45 then
            result &:= "\n" & str(chr(32 + length(stri) - index - 2));
          else
            result &:= "\nM";
          end if;
          posToAddNewline +:= 45;
        end if;
      end for;
      result &:= "\n";
    end if;
    result &:= "`\n";
  end func;

Decode an uuencoded string

This function is part of the "encoding.s7i" library.

const func string: fromUuencoded (in string: stri) is func
  result
    var string: result is "";
  local
    var integer: lineLength is 1;
    var integer: index is 1;
    var integer: subIndex is 1;
    var integer: number is 0;
    var integer: fourBytes is 0;
    var string: threeBytes is "   ";
  begin
    lineLength := ord(stri[1]) - 32;
    while lineLength <> 0 and lineLength <> 64 do
      incr(index);
      while lineLength >= 1 do
        fourBytes := 0;
        for subIndex range index to index + 3 do
          number := ord(stri[subIndex]) - 32;
          if number = 64 then
            number := 0;
          elsif number < 0 or number > 64 then
            raise RANGE_ERROR;
          end if;
          fourBytes := (fourBytes << 6) + number;
        end for;
        threeBytes @:= [1] chr( fourBytes >> 16);
        threeBytes @:= [2] chr((fourBytes >>  8) mod 256);
        threeBytes @:= [3] chr( fourBytes        mod 256);
        result &:= threeBytes[ .. lineLength];
        lineLength -:= 3;
        index +:= 4;
      end while;
      while index <= length(stri) and stri[index] <> '\n' do
        incr(index);
      end while;
      if index < length(stri) then
        incr(index);
        lineLength := ord(stri[index]) - 32;
      else
        lineLength := 0;
      end if;
    end while;
  end func;

Compress a string using the Lempel Ziv Welch (LZW) compression

This algorithm compresses a byte string to a string of tokens which contains characters >= 256. To write this to a byte file it is necessary to add an algorithm which writes the tokens with 9 or more bits.

const func string: lzwCompress (in string: uncompressed) is func
  result
    var string: result is "";
  local
    var char: ch is ' ';
    var hash [string] char: mydict is (hash [string] char).value;
    var string: buffer is "";
    var string: xstr is "";
  begin
    for ch range chr(0) to chr(255) do
      mydict @:= [str(ch)] ch;
    end for;
    for ch range uncompressed do
      if buffer = "" then
        xstr := str(ch);
      else
        xstr := buffer & str(ch);
      end if;
      if xstr in mydict then
        buffer &:= str(ch)
      else
        result &:= str(mydict[buffer]);
        mydict @:= [xstr] chr(length(mydict));
        buffer := str(ch);
      end if;
    end for;
    if buffer <> "" then
      result &:= str(mydict[buffer]);
    end if;
  end func;

Uncompress a Lempel Ziv Welch (LZW) compressed string

The compressed string consists of a sequence of tokens (which contain also characters >= 256). The uncompress algorithm produces a byte string.

const func string: lzwUncompress (in string: compressed) is func
  result
    var string: result is "";
  local
    var char: ch is ' ';
    var hash [char] string: mydict is (hash [char] string).value;
    var string: buffer is "";
    var string: current is "";
    var string: chain is "";
  begin
    for ch range chr(0) to chr(255) do
      mydict @:= [ch] str(ch);
    end for;
    for ch range compressed do
      if buffer = "" then
        buffer := mydict[ch];
        result &:= buffer;
      elsif ch <= chr(255) then
        current := mydict[ch];
        result &:= current;
        chain := buffer & current;
        mydict @:= [chr(length(mydict))] chain;
        buffer := current;
      else
        if ch in mydict then
          chain := mydict[ch];
        else
          chain := buffer & str(buffer[1]);
        end if;
        result &:= chain;
        mydict @:= [chr(length(mydict))] buffer & str(chain[1]);
        buffer := chain;
      end if;
    end for;
  end func;

Compress a string using the run length encoding of bzip2

A sequence of 4 to 259 identical characters is replaced by four identical characters followed by a repeat length between 0 and 255. This run length encoding is used as first compression technique in the bzip2 compression.

const func string: bzip2RleCompress (in string: stri) is func
  result
    var string: result is "";
  local
    var integer: index is 1;
    var integer: oldpos is 1;
    var char: ch is ' ';
  begin
    while index <= length(stri) - 3 do
      ch := stri[index];
      if  stri[succ(index)] = ch and
          stri[index + 2] = ch and
          stri[index + 3] = ch then
        index +:= 4;
        result &:= stri[oldpos .. pred(index)];
        oldpos := index;
        while index <= length(stri) and
            stri[index] = ch do
          incr(index);
        end while;
        result &:= str(chr(index - oldpos));
        oldpos := index;
      else
        incr(index);
      end if;
    end while;
    result &:= stri[oldpos ..];
  end func;

Decompress a string using the run length encoding of bzip2

A sequence of 4 identical characters followed by a repeat length between 0 and 255 is replaced by 4 to 259 identical characters. This run length decoding is used as last decompression technique in the bzip2 decompression.

const func string: bzip2RleUncompress (in string: stri) is func
  result
    var string: result is "";
  local
    var integer: index is 1;
    var integer: oldpos is 1;
    var char: ch is ' ';
  begin
    while index <= length(stri) - 3 do
      ch := stri[index];
      if  stri[succ(index)] = ch and
          stri[index + 2] = ch and
          stri[index + 3] = ch then
        index +:= 4;
        result &:= stri[oldpos .. pred(index)] & str(ch) mult ord(stri[index]);
        incr(index);
        oldpos := index;
      else
        incr(index);
      end if;
    end while;
    result &:= stri[oldpos ..];
  end func;

Compress a string using the run length encoding of PackBits

const func string: packBits (in string: stri) is func
  result
    var string: result is "";
  local
    var integer: index is 1;
    var integer: oldpos is 1;
    var char: ch is ' ';
  begin
    while index <= length(stri) - 2 do
      ch := stri[index];
      if  stri[succ(index)] = ch and
          stri[index + 2] = ch then
        while index - oldpos >= 128 do
          result &:= "\127\" & stri[oldpos len 128];
          oldpos +:= 128;
        end while;
        if index > oldpos then
          result &:= str(chr(pred(index - oldpos))) & stri[oldpos .. pred(index)];
          oldpos := index;
        end if;
        index +:= 3;
        while index <= length(stri) and stri[index] = ch do
          incr(index);
        end while;
        while index - oldpos >= 128 do
          result &:= "\129\" & str(ch);
          oldpos +:= 128;
        end while;
        if pred(index) > oldpos then
          result &:= str(chr(257 - (index - oldpos))) & str(ch);
          oldpos := index;
        end if;
      else
        incr(index);
      end if;
    end while;
    index := succ(length(stri));
    while index - oldpos >= 128 do
      result &:= "\127\" & stri[oldpos len 128];
      oldpos +:= 128;
    end while;
    if index > oldpos then
      result &:= str(chr(pred(index - oldpos))) & stri[oldpos ..];
    end if;
  end func;

Decompress a string using the run length encoding of PackBits

const func string: unpackBits (in string: stri) is func
  result
    var string: result is "";
  local
    var integer: index is 1;
    var integer: oldpos is 1;
    var char: ch is ' ';
  begin
    while index <= length(stri) do
      ch := stri[index];
      if ch <= chr(127) then
        result &:= stri[succ(index) len succ(ord(ch))];
        index +:= ord(ch) + 2;
      else
        result &:= str(stri[succ(index)]) mult (257 - ord(ch));
        index +:= 2;
      end if;
    end while;
  end func;

Burrows-Wheeler transform (basic concept)

This algorithm demonstrates the basic concept of the Burrows-Wheeler transform. It is not intended to be used for big data blocks.

const func string: burrowsWheelerTransformConcept (in string: stri) is func
  result
    var string: result is "";
  local
    var integer: length is 0;
    var integer: index is 0;
    var array string: rotations is 0 times "";
  begin
    length := succ(length(stri));
    rotations := length times "";
    for index range 1 to length do
      rotations[index] := stri[index ..] & "\256\" & stri[.. pred(index)];
    end for;
    rotations := sort(rotations);
    for index range 1 to length do
      result &:= str(rotations[index][length]);
    end for;
  end func;

Inverse Burrows-Wheeler transform (basic concept)

This algorithm demonstrates the basic concept of the Burrows-Wheeler transform. It is not intended to be used for big data blocks.

const func string: inverseBurrowsWheelerTransformConcept (in string: stri) is func
  result
    var string: result is "";
  local
    var integer: length is 0;
    var integer: count is 0;
    var integer: index is 0;
    var array string: rotations is 0 times "";
  begin
    length := length(stri);
    rotations := length times "";
    for count range 1 to length do
      for index range 1 to length do
        rotations[index] := str(stri[index]) & rotations[index];
      end for;
      rotations := sort(rotations);
    end for;
    result := rotations[1];
    index := pos(result, "\256\");
    result := result[succ(index) ..] & result[.. pred(index)];
  end func;

Convert a character string to morse code

const func string: charToMorse (in char: ch) is func
  result
    var string: result is "";
  begin
    case ch of
      when {'a', 'A'}: result := ".-";
      when {'b', 'B'}: result := "-...";
      when {'c', 'C'}: result := "-.-.";
      when {'d', 'D'}: result := "-..";
      when {'e', 'E'}: result := ".";
      when {'f', 'F'}: result := "..-.";
      when {'g', 'G'}: result := "--.";
      when {'h', 'H'}: result := "....";
      when {'i', 'I'}: result := "..";
      when {'j', 'J'}: result := ".---";
      when {'k', 'K'}: result := "-.-";
      when {'l', 'L'}: result := ".-..";
      when {'m', 'M'}: result := "--";
      when {'n', 'N'}: result := "-.";
      when {'o', 'O'}: result := "---";
      when {'p', 'P'}: result := ".--.";
      when {'q', 'Q'}: result := "--.-";
      when {'r', 'R'}: result := ".-.";
      when {'s', 'S'}: result := "...";
      when {'t', 'T'}: result := "-";
      when {'u', 'U'}: result := "..-";
      when {'v', 'V'}: result := "...-";
      when {'w', 'W'}: result := ".--";
      when {'x', 'X'}: result := "-..-";
      when {'y', 'Y'}: result := "-.--";
      when {'z', 'Z'}: result := "--..";
      when {'Ä', 'Æ'}: result := ".-.-";
      when {'À', 'Å'}: result := ".--.-";
      when {'Ç', 'Ĉ'}: result := "-.-..";
      when {'Ð'}:      result := "..--.";
      when {'È'}:      result := ".-..-";
      when {'É'}:      result := "..-..";
      when {'Ĝ'}:      result := "--.-.";
      when {'Ĵ'}:      result := ".---.";
      when {'Ñ'}:      result := "--.--";
      when {'Ö', 'Ø'}: result := "---.";
      when {'Ŝ'}:      result := "...-.";
      when {'Ü', 'Ŭ'}: result := "..--";
      when {'Þ'}:      result := ".--..";
      when {'0'}:      result := "-----";
      when {'1'}:      result := ".----";
      when {'2'}:      result := "..---";
      when {'3'}:      result := "...--";
      when {'4'}:      result := "....-";
      when {'5'}:      result := ".....";
      when {'6'}:      result := "-....";
      when {'7'}:      result := "--...";
      when {'8'}:      result := "---..";
      when {'9'}:      result := "----.";
      when {'!'}:      result := "-.-.--";
      when {'"'}:      result := ".-..-.";
      when {'$'}:      result := "...-..-";
      when {'''}:      result := ".----.";
      when {'('}:      result := "-.--.";
      when {')'}:      result := "-.--.-";
      when {'+'}:      result := ".-.-.";
      when {','}:      result := "--..--";
      when {'-'}:      result := "-....-";
      when {'.'}:      result := ".-.-.-";
      when {'/'}:      result := "-..-.";
      when {':'}:      result := "---...";
      when {';'}:      result := "-.-.-.";
      when {'='}:      result := "-...-";
      when {'?'}:      result := "..--..";
      when {'@'}:      result := ".--.-.";
      when {' '}:      result := " ";
    end case;
  end func;

const func string: stringToMorse (in string: stri) is func
  result
    var string: result is "";
  local
    var char: ch is ' ';
  begin
    for ch range stri do
      result &:= charToMorse(ch) & " ";
    end for;
  end func;

Convert morse code to a character string

const func char: morseToChar (in string: morseLetter) is func
  result
    var char: result is ' ';
  begin
    if    morseLetter = "" then        result := ' ';
    elsif morseLetter = "." then       result := 'E';
    elsif morseLetter = "-" then       result := 'T';
    elsif morseLetter = ".." then      result := 'I';
    elsif morseLetter = ".-" then      result := 'A';
    elsif morseLetter = "-." then      result := 'N';
    elsif morseLetter = "--" then      result := 'M';
    elsif morseLetter = "..." then     result := 'S';
    elsif morseLetter = "..-" then     result := 'U';
    elsif morseLetter = ".-." then     result := 'R';
    elsif morseLetter = ".--" then     result := 'W';
    elsif morseLetter = "-.." then     result := 'D';
    elsif morseLetter = "-.-" then     result := 'K';
    elsif morseLetter = "--." then     result := 'G';
    elsif morseLetter = "---" then     result := 'O';
    elsif morseLetter = "...." then    result := 'H';
    elsif morseLetter = "...-" then    result := 'V';
    elsif morseLetter = "..-." then    result := 'F';
    elsif morseLetter = "..--" then    result := 'Ü'; # also 'Ŭ'
    elsif morseLetter = ".-.." then    result := 'L';
    elsif morseLetter = ".-.-" then    result := 'Ä'; # also 'Æ'
    elsif morseLetter = ".--." then    result := 'P';
    elsif morseLetter = ".---" then    result := 'J';
    elsif morseLetter = "-..." then    result := 'B';
    elsif morseLetter = "-..-" then    result := 'X';
    elsif morseLetter = "-.-." then    result := 'C';
    elsif morseLetter = "-.--" then    result := 'Y';
    elsif morseLetter = "--.." then    result := 'Z';
    elsif morseLetter = "--.-" then    result := 'Q';
    elsif morseLetter = "---." then    result := 'Ö'; # also 'Ø'
    elsif morseLetter = "----" then    result := ' '; # 'ch'
    elsif morseLetter = "....." then   result := '5';
    elsif morseLetter = "....-" then   result := '4';
    elsif morseLetter = "...-." then   result := 'Ŝ';
    elsif morseLetter = "...--" then   result := '3';
    elsif morseLetter = "..-.." then   result := 'É';
    elsif morseLetter = "..-.-" then   result := ' '; # unused
    elsif morseLetter = "..--." then   result := 'Ð';
    elsif morseLetter = "..---" then   result := '2';
    elsif morseLetter = ".-..." then   result := ' '; # unused
    elsif morseLetter = ".-..-" then   result := 'È';
    elsif morseLetter = ".-.-." then   result := '+';
    elsif morseLetter = ".-.--" then   result := ' '; # unused
    elsif morseLetter = ".--.." then   result := 'Þ';
    elsif morseLetter = ".--.-" then   result := 'À'; # also 'Å'
    elsif morseLetter = ".---." then   result := 'Ĵ';
    elsif morseLetter = ".----" then   result := '1';
    elsif morseLetter = "-...." then   result := '6';
    elsif morseLetter = "-...-" then   result := '=';
    elsif morseLetter = "-..-." then   result := '/';
    elsif morseLetter = "-..--" then   result := ' '; # unused
    elsif morseLetter = "-.-.." then   result := 'Ç'; # also 'Ĉ'
    elsif morseLetter = "-.-.-" then   result := ' '; # Start of message
    elsif morseLetter = "-.--." then   result := '('; # also 'Ĥ'
    elsif morseLetter = "-.---" then   result := ' '; # unused
    elsif morseLetter = "--..." then   result := '7';
    elsif morseLetter = "--..-" then   result := ' '; # unused
    elsif morseLetter = "--.-." then   result := 'Ĝ';
    elsif morseLetter = "--.--" then   result := 'Ñ';
    elsif morseLetter = "---.." then   result := '8';
    elsif morseLetter = "---.-" then   result := ' '; # unused
    elsif morseLetter = "----." then   result := '9';
    elsif morseLetter = "-----" then   result := '0';
    elsif morseLetter = "..--.." then  result := '?';
    elsif morseLetter = ".-..-." then  result := '"';
    elsif morseLetter = ".-.-.-" then  result := '.';
    elsif morseLetter = ".--.-." then  result := '@';
    elsif morseLetter = ".----." then  result := ''';
    elsif morseLetter = "-....-" then  result := '-';
    elsif morseLetter = "-.-.--" then  result := '!';
    elsif morseLetter = "-.-.-." then  result := ';';
    elsif morseLetter = "-.--.-" then  result := ')';
    elsif morseLetter = "--..--" then  result := ',';
    elsif morseLetter = "---..." then  result := ':';
    elsif morseLetter = "...-..-" then result := '$';
    else                               result := ' ';
    end if;
  end func;

const func string: morseToString (in string: stri) is func
  result
    var string: result is "";
  local
    var array string: letters is 0 times "";
    var string: letter is "";
  begin
    letters := split(replace(stri, "  ", " "), ' ');
    for letter range letters do
      result &:= str(morseToChar(letter));
    end for;
  end func;

Wildcard match used in command shells

const func boolean: wildcard_match (in string: main_stri, in string: pattern) is func
  result
    var boolean: result is FALSE;
  local
    var integer: main_length is 0;
    var integer: main_index is 1;
    var string: pattern_tail is "";
  begin
    if pattern = "" then
      result := main_stri = "";
    else
      case pattern[1] of
        when {'*'}:
          if pattern = "*" then
            result := TRUE;
          else
            main_length := length(main_stri);
            pattern_tail := pattern[2 .. ];
            while main_index <= main_length and not result do
              result := wildcard_match(main_stri[main_index .. ],
                  pattern_tail);
              incr(main_index);
            end while;
          end if;
        when {'?'}:
          if main_stri <> "" then
            result := wildcard_match(main_stri[2 .. ], pattern[2 .. ]);
          end if;
        otherwise:
          if main_stri <> "" and main_stri[1] = pattern[1] then
            result := wildcard_match(main_stri[2 .. ], pattern[2 .. ]);
          end if;
      end case;
    end if;
  end func;


 previous   up   next