Page 1 of 1

Strings, strings and strings

Posted: Fri Nov 26, 2021 3:47 pm
by frank
SharpBASIC supports three types of strings:
  • variable length, which are null-terminated
  • fixed length, which are not null-terminated
  • constant strings
Constant strings of course cannot be changed, but they can be assigned to fixed and variable length strings. However, fixed and variable length strings can be assigned to each other. Consider the following program:

Code: Select all

' SharpBASIC stre00
' -------------------
' option strict hint;

incl "lib/sys.sbi";

dim is
  s0: str * 24 = "The apple tree is empty.";
  s1: str;
  s2: str = "Next year is a new round.";
end;

main do

  s1 = s0;
  print(s1);
  
  s1 = s2;
  print(s1);

end;
Output:
Image

In this example, the fixed length string s0 is copied to s1. The main difference of these two string types lies in the string descriptor, which in SharpBASIC has the format [addr:size].

The size in the descriptor of a variable length string is always null, which tells the compiler that the string pointed to by the address in the descriptor is a null-terminated string. By contrast, the size in the descriptor of a fixed length string is always the actual fixed size of the string.

Constant strings in SharpBASIC do not have a descriptor and are null-terminated.

Re: Strings, strings and strings

Posted: Fri Nov 26, 2021 11:13 pm
by frank
Another example program assigning mixed string types.

Code: Select all

' SharpBASIC stre02
' -----------------
incl "lib/sys.sbi";

const c = "Tell me,";

dim s1: str * 12;
dim s2: str * 7;
dim s3: str = "does it fit?";

main do

  s1 = c;
  print(s1);

  s1 = s3;
  print(s1);

  s2 = s3;
  print(s2);

  s3 = s1;
  print(s3);

end;
Image

Re: Strings, strings and strings

Posted: Sat Nov 27, 2021 6:44 pm
by aurel
what kind of distro is that
Ubuntu ..Debian
just asking..

Re: Strings, strings and strings

Posted: Sun Nov 28, 2021 11:10 am
by frank
aurel wrote: Sat Nov 27, 2021 6:44 pm what kind of distro is that
Ubuntu ..Debian
just asking..
Manjaro XFCE. I use the OS extensively for development.