Strings, strings and strings

Follow the development of the SharpBASIC compiler and share your thoughts and ideas.
Locked
User avatar
frank
Site Admin
Posts: 40
Joined: Sun Nov 21, 2021 12:04 pm
Location: Netherlands
Contact:

Strings, strings and strings

Post 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.
Keep it simple!
User avatar
frank
Site Admin
Posts: 40
Joined: Sun Nov 21, 2021 12:04 pm
Location: Netherlands
Contact:

Re: Strings, strings and strings

Post 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
Keep it simple!
aurel
Posts: 11
Joined: Tue Nov 23, 2021 12:18 pm

Re: Strings, strings and strings

Post by aurel »

what kind of distro is that
Ubuntu ..Debian
just asking..
User avatar
frank
Site Admin
Posts: 40
Joined: Sun Nov 21, 2021 12:04 pm
Location: Netherlands
Contact:

Re: Strings, strings and strings

Post 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.
Keep it simple!
Locked