Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:30 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2: What is LONGSTRING RESIZE trying to do (bug?)

Author Message
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 02:21pm 05 Sep 2021
Copy link to clipboard 
Print this post

Found whilst writing unit-tests for Linux MMBasic:

On the CMM2:

> list "longstring_resize.bas"
Option Base 0
Option Default None
Option Explicit On

Dim lstr%(100)
Print "Should be 0, is" LLen(lstr%())
LongString Append lstr%(), "Hello World"
Print "Should be 11, is" LLen(lstr%())
LongString Resize lstr%(), 4
Print "Should be 4 (?), is" LLen(lstr%())

> run "longstring_resize.bas"
Should be 0, is 0
Should be 11, is 11
Should be 4 (?), is 5


Also note that the documentation for LONGSTRING RESIZE in the manual is incorrect as it is missing the array%() parameter, ditto LONGSTRING SETBYTE.

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 04:26pm 05 Sep 2021
Copy link to clipboard 
Print this post

This is the faulty line

       dest[0] = getint(argv[2],OptionBase,j-OptionBase)+1;


should probably be
       dest[0] = getint(argv[2],OptionBase,j+OptionBase );


You might want to check the use of OptionBase throughout the longstring commands

GetByte and SetByte are byte array index operations and should respect OptionByte
Everything else are string operations where the first element is always one and OptionByte is irrelevant (except that it determines the size of the array and hence how many bytes are available)

The bug is relatively benign and resize was really only there to allow setbyte on a newly created zero length longstring
Edited 2021-09-06 02:29 by matherp
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 10:45pm 05 Sep 2021
Copy link to clipboard 
Print this post

  matherp said  should probably be
dest[0] = getint(argv[2],OptionBase,j+OptionBase);



Don't you mean:
dest[0] = getint(argv[2], 0, j)

?

As the minimum number of bytes/characters is always 0 and the maximum number is always 8 * (number of elements in the array - 1).

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 07:11am 06 Sep 2021
Copy link to clipboard 
Print this post

  Quote  As the minimum number of bytes/characters is always 0 and the maximum number is always 8 * (number of elements in the array - 1).


this is the confusion of option base. I wish it didn't exist as it causes so much special case code. Does resize correspond to array indicies (e.g 1-8) or the number of slots (e.g 0-8)? Does 0 make sense? is it the same as clear?
Edited 2021-09-06 17:15 by matherp
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 08:30am 06 Sep 2021
Copy link to clipboard 
Print this post

  matherp said  ... this is the confusion of option base. I wish it didn't exist as it causes so much special case code.


You and me both. I hate it, and its partner in crime the "sizes" specified when arrays are declared being the maximum bound instead of the (sane to me) number of elements .

  Quote  Does resize correspond to array indicies (e.g 1-8) or the number of slots (e.g 0-8)? Does 0 make sense? is it the same as clear?


My opinion is that it is the number of byte/character elements and as a result any value that could be returned by LLEN should be a valid argument to LONGSTRING RESIZE hence 0 does make sense even though it is the same as LONGSTRING CLEAR ... otherwise the client code has to special case for 0 when it comes up as the result of a calculation.

I would also argue that LONGSTRING TRIM should behave the same, currently it doesn't allow 1 and doesn't allow you to trim the entire string or beyond. I feel these "arbitrary" restrictions just make writing the client-code more difficult. However YMMV and I don't want the behaviour of MMBasic for Linux to differ as that just compounds the problem.

Best wishes,

Tom

P.S. with the benefit of hindsight you could probably make an argument that LONGSTRING SETBYTE and LGETBYTE don't really belong in the language at all since we have POKE and PEEK. I guess there is a slight performance improvement.
Edited 2021-09-06 18:44 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
jirsoft

Guru

Joined: 18/09/2020
Location: Czech Republic
Posts: 533
Posted: 10:20am 06 Sep 2021
Copy link to clipboard 
Print this post

  thwill said  You and me both. I hate it, and its partner in crime the "sizes" specified when arrays are declared being the maximum bound instead of the (sane to me) number of elements .

I find it not so bad for 2 reasons:
1. I don't need everytime in the loop calculate size-1 as TO limit. (You haven't this problem in C as the upper for for loop is not reachable)
2. I'm programming with OPTION BASE 0, but sometimes not using first element (0), so I have this way eliminated MMBasic's problem with one element array...
Jiri
Napoleon Commander and SimplEd for CMM2 (GitHub),  CMM2.fun
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 11:22am 06 Sep 2021
Copy link to clipboard 
Print this post

Tom

Change your code to

dest[0] = getint(argv[2], 0, j)


I'll do the same if/when there is another CMM2 release
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 01:56pm 06 Sep 2021
Copy link to clipboard 
Print this post

  matherp said  Tom

Change your code to

dest[0] = getint(argv[2], 0, j)


I'll do the same if/when there is another CMM2 release


Than you Peter, I will do so.

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025