RosettaCodeData/Task/Binary-strings/M2000-Interpreter/binary-strings.m2000

52 lines
2.0 KiB
Plaintext

' String assignment
a$="alfa"
string a="beta"
b="beta too"
all=a$+a+b
Print All
' String comparison
Print a$=a, a=b, a+A+" too"+a$=a+b+a$ ' false false true
b$=a$
// compare return 0 for equal (-1,0, 1),
//same as <=> but compare work with variables only (without coping values)
print "using compare", compare(a$, b$), a$<=>b$ ' 0 0
' String cloning and copying
a2$=a$ ' copy
' Check if a string is empty
print a2$="", len(a$)=0
' Append a byte to a string
c$=Str$("ABCDE") ' utf16le "ABCDE" convert to ANSI (one byte per code)
Print len(c$)=2.5 ' measure 16bit, so we get 2.5x2=5 bytes
c$+=Str$("Z")
Print Len(c$)=3
Print chr$(c$)="ABCDEZ" ' convert to UTF16LE and then compare
' Extract a substring from a string
Print chr$(mid$(c$,2, 2 as byte))="BC"
Print mid$("ABCDEZ",2,2) ="BC"
' Replace every occurrence of a byte (or a string) in a string with another string
Print Replace$("BC", "?", "ABCDEZBC")="A?DEZ?"
Print Replace$("BC", "?", CHR$(c$))="A?DEZ"
' String creation and destruction (using buffers. Memory Blocks)
' (when needed and if there's no garbage collection or similar mechanism)
'Join strings (IN A BUFFRT)
buffer clear K as byte*10 ' using clear to erase the buffer with 0
Return K, 0:=c$
Print eval(k, 2)=asc("C"), eval(k, 3)=asc("D")
Return k, 2:=str$("KL")
Print chr$(eval$(k, 0, 6))="ABKLEZ"
' rotate one byte
M= eval(K,0)
return k, 0:=eval$(k, 1, 5), 5:=m ' we can place strings or bytes at specific index - from 0.
Print chr$(eval$(k, 0, 6))="BKLEZA" ' true
// change length of buffer
buffer K as byte*6
// now we get the full string (with the length of 6 bytes)
Print chr$(eval$(k))="BKLEZA" ' true
buffer K1 as byte*6
// buffers are special objects, so we have strings refering by object pointer.
// we can pass the buffer pointer or return it from a function, also we can swap variables for buffers
swap K, k1
Print chr$(eval$(k1))="BKLEZA" ' true
// we can get the address of string:
Print k1(0) ' return the absolute address of byte at offset 0 on k1 buffer