[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

String Concatenation



I added the string concatenation thingy to OOC.  Things went quite
smoothly, the necessary changes were minimal.  Here is a summary of
OOC's handling of character and string constants:

 o string constants of length one and character constants are
   equivalent as far as the compiler is concerned; that is, "A" and
   41X are the same

 o the empty string and the character 0X are interchangeable

 o the string concatenation operator `+' takes two string constants as
   arguments, and the result is a string constant

These rules imply the following:

 o string concatenation does not apply to variables of type "ARRAY [n]
   OF CHAR"

 o concatenation with the character constant 0X is equivalent to
   concatenation with the empty string ""; this means one cannot build
   a string constant with an embedded 0X character

 o concatenation of any two character constants delivers a string
   result, which is no longer equivalent to a character constant; that
   is, "a" and "a"+"" are _not_ interchangeable, because the first can
   be used as a character value, but not the second

See the attached test program and its output for examples.  If
anything of this surprises you, feel free to complain on the mailing
list.

-- mva


MODULE TestString;  <* ConformantMode := FALSE *>

IMPORT
  Out, Strings;
  
PROCEDURE Char (msg: ARRAY OF CHAR; ch: CHAR);
  BEGIN
    Out.String (msg); Out.String (": "); Out.Char (ch);
    Out.String (" ("); Out.Hex (ORD (ch), 2); Out.String ("X)"); Out.Ln
  END Char;

PROCEDURE String (msg: ARRAY OF CHAR; str: ARRAY OF CHAR);
  BEGIN
    Out.String (msg); Out.String (": "); Out.String (str);
    Out.String (" (length="); Out.Int (Strings.Length (str), 0); 
    Out.String (")"); Out.Ln
  END String;

BEGIN
  Char ("empty string", "");
  Char ("char 0X", 0X);
  String ("empty string", "");
  String ("char 0X", 0X);

  String ("simple concat", "a"+"b");
  String ("simple concat", "aa"+"bb");
  String ("simple concat char", "a"+63X);
  String ("simple concat char", "aa"+63X+63X);
  String ("concat with empty", "a"+"");
  String ("concat with empty", "aa"+"");
  String ("concat with empty char", "a"+0X);
  String ("concat with empty char", "aa"+0X);
  String ("concat with empty char", 0X + "a");
  String ("concat with empty char", 0X + "aa");
  
  String ("empty concat", ""+"");
  String ("empty concat", 0X+0X);
  (*Char ("empty concat", ""+"");  illegal, result of ""+"" is string const *)
  (*Char ("empty concat", 0X+0X);  illegal, result of 0X+0X is string const *)
  
  String ("triple concat", "a"+"b"+"c");
  String ("triple concat", ("a"+"b")+"c");
  String ("triple concat", "a"+("b"+"c"));
  String ("triple concat", "a"+""+"z");
  String ("triple concat", "a"+0X+"z");
END TestString.


~/ooc$ ./TestString 
empty string:  (00X)
char 0X:  (00X)
empty string:  (length=0)
char 0X:  (length=0)
simple concat: ab (length=2)
simple concat: aabb (length=4)
simple concat char: ac (length=2)
simple concat char: aacc (length=4)
concat with empty: a (length=1)
concat with empty: aa (length=2)
concat with empty char: a (length=1)
concat with empty char: aa (length=2)
concat with empty char: a (length=1)
concat with empty char: aa (length=2)
empty concat:  (length=0)
empty concat:  (length=0)
triple concat: abc (length=3)
triple concat: abc (length=3)
triple concat: abc (length=3)
triple concat: az (length=2)
triple concat: az (length=2)