Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 13:02 23 Nov 2024 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 : extension TRIM

Author Message
georgestheking
Newbie

Joined: 21/12/2021
Location: Belgium
Posts: 26
Posted: 09:22am 24 Sep 2024
Copy link to clipboard 
Print this post

Hi,

A TRIM commande will be nice.
It remove space before and afer a string.

Best regards

Georges
 
Pluto
Guru

Joined: 09/06/2017
Location: Finland
Posts: 357
Posted: 09:37am 24 Sep 2024
Copy link to clipboard 
Print this post

A good starting point is to read/search in the PicoMite User manual. You will find TRIM there. (Also SELECT/CASE as you asked for in an other thread.)

Pluto
 
Pluto
Guru

Joined: 09/06/2017
Location: Finland
Posts: 357
Posted: 09:40am 24 Sep 2024
Copy link to clipboard 
Print this post

Link to User manual
Download links at the bottom of the page.
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1236
Posted: 10:01am 24 Sep 2024
Copy link to clipboard 
Print this post

For MicroMites: https://www.thebackshed.com/forum/ViewTopic.php?TID=9738

I actually thought that TRIM - an extended TRIM with more options - was already part of the MMBasics for PicoMites and CMM2. It sucks when you get old...


I found the code for a TRIM-CSUB in my CMM2 folder:
#include "ARMCFunctions.h"
#define NULL (void *) 0

void trim(char* string, char* b_char) {
// combines rTrim and lTrim
// Note: doesn't work for string literals
// eg trim("   test"," ")
unsigned char len = (unsigned char)*string;
unsigned char i;
unsigned char spaces = 0;
char* stemp = string;// Save string pointer
char bc = ' ';       // blank char = ' '

if (string == NULL)  // no args = nothing to do
return;

if (b_char != NULL ) // no blank char arg? Then use default
 bc = b_char[1];

//-------------------------rtrim
stemp = stemp+len; // point to the end of the string
// Find the first non-space at the end of the string
while (spaces <= len && *stemp-- == bc) {
   ++spaces;
}
len -= spaces; // Remaining string length = len - spaces
//------------------------ltrim
spaces = 0;
stemp = string;
// Search first non-space character
while (spaces < len && *++stemp == bc) {
++spaces;
}
len    -= spaces;    // Remaining string length = len - spaces
*string = (char)len; // Store BASIC (trimmed) string length
for (i=0;i++ <len;) {// Copy the rest of the string
string[i] = string[i+spaces];
}
return;
}

It should be easy to build a CSUB for PicoMite based on that.
I forgot if I wrote the code.
Edited 2024-09-24 20:28 by twofingers
causality ≠ correlation ≠ coincidence
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6768
Posted: 10:37am 24 Sep 2024
Copy link to clipboard 
Print this post

I've very rarely used it. It's hardly worth building in.  :)


t$ = "12345  6"
PRINT t$
PRINT trim$(t$)

FUNCTION TRIM$(a$)
TRIM$ = LEFT$(a$,INSTR(a$," ")-1)
END FUNCTION


run
12345  6
12345
>
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4036
Posted: 10:45am 24 Sep 2024
Copy link to clipboard 
Print this post

If/when we have 2-byte function tokens then TRIM$() may be a possible addition, until then DIY.

Best wishes,

Tom
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2350
Posted: 10:58am 24 Sep 2024
Copy link to clipboard 
Print this post

  thwill said  If/when we have 2-byte function tokens [...]


hi Tom,
   do we not have 16-bit function tokens on any of the mmbasic platforms yet? i've not been following developments too much recently.


cheers,
rob   :-)
Edited 2024-09-24 20:59 by robert.rozee
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1236
Posted: 11:09am 24 Sep 2024
Copy link to clipboard 
Print this post

CSUB for Picomites if speed matters:

a$="   12345    "
Print a$, Len(a$)
ctrim a$, " "
Print a$, Len(a$)

'File ctrim.bas written 24-09-2024 12:51:00 v1.44
CSub ctrim string string
 00000000
 46C6B5F0 7804B500 D0632900 23007849 78151902 428D3A01 3301D103 429CB2DB
 1AE4D2F7 0002B2E4 2C002300 E05CD104 B2DB3301 D04A429C 32017855 D0F7428D
 B2E21AE4 70024690 D03D2A00 00142200 18411D59 428D1C45 1C5F4154 19C60021
 42A61D44 430A4152 39014641 468CB2C9 45612108 42494189 D02D4211 4332002A
 D1290792 21004642 22000897 32015874 506CB2D2 42973104 2203D1F8 43944644
 00221C61 45A0B2C9 18C3D00E 54455C5D B2ED1CA5 D9074588 32035D59 B2D25541
 D90145A8 54835C9B 46B8BC80 2300BDF0 E7F97003 E79A2120 19C02300 54EA5CC2
 B2DA3301 D8F94590 7004E7EE 46C0E7EC
End CSub

causality ≠ correlation ≠ coincidence
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4036
Posted: 11:35am 24 Sep 2024
Copy link to clipboard 
Print this post

  robert.rozee said  hi Tom,
   do we not have 16-bit function tokens on any of the mmbasic platforms yet? i've not been following developments too much recently.


Hi Rob,

No, no 16-bit function tokens yet on any MMBasic platform.

Thanks largely to Peter (plus a little tidying by me ) the PicoMites and MMB4L have 16-bit command tokens so we are no longer limited to ~128 commands. I don't know what the case with the CMM2 or MMBasic for Windows is.

I *believe* Peter had a look at implementing 16-bit function tokens on the PicoMite at about the same time and concluded it was more involved and that he would rather not tackle it (yet).

I think the *plan* is now that once MMB4L 0.7.0 is "done" that I will try and add 16-bit function token support to MMB4L and assuming I'm successful then either Peter or I will port the changes to the PicoMite.

HOWEVER we are relying on me so we could be looking at 6 months or more, so it's very possible that Peter will get bored waiting and do it himself ... there is certainly precedence .

Best wishes,

Tom
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1236
Posted: 01:59pm 24 Sep 2024
Copy link to clipboard 
Print this post

CSUB for CMM2:
https://www.thebackshed.com/forum/ViewTopic.php?FID=16&TID=12734

Just in case ...
causality ≠ correlation ≠ coincidence
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2350
Posted: 03:18pm 24 Sep 2024
Copy link to clipboard 
Print this post

  thwill said  
  robert.rozee said  hi Tom,
   do we not have 16-bit function tokens on any of the mmbasic platforms yet? i've not been following developments too much recently.


No, no 16-bit function tokens yet on any MMBasic platform.


a simple approach would be to use a 'pre-processor'. as an example:

1. remove the functions SIN(, COS(, TAN(, ASIN(, ACOS(, ATAN( - as i recall, the opening bracket is hard-coded into the function name within the interpreter. this 'frees up' six function tokens.

2. create a new function called MM.TRIG(n, x) where n selects between six different options for what to return: calculated value of sin(x), cos(x), tan(x), asin(x), acos(x), atan(x).

3. use a pre-processor that runs across the BASIC source code before it is tokenized and makes the substitutions:
SIN(   -->  MM.TRIG(1,
COS(   -->  MM.TRIG(2,
TAN(   -->  MM.TRIG(3,
ASIN(   -->  MM.TRIG(4,
ACOS(   -->  MM.TRIG(5,
ATAN(   -->  MM.TRIG(6,

4. whenever the interpreter generates an error output, scan for occurrences of MM.TRIG(n, in the error output and substitute back in the appropriate original function names based on the value of n, after which the error output can then be displayed.


the above can in fact be expanded to a larger group of functions, the only requirement is that the group of functions need to all share the same number and types of input parameters, and return the same type of output. so you can group together sin, cos, tan, asin, acos, atan, abs, cint, deg, exp, fix, int, log, rad, sgn, sqr, along with the hyperbolic trig functions currently handled by MATH ...n,  

another candidate group for the same treatment would be inkey$, date$, time$, cwd$.


cheers,
rob   :-)
Edited 2024-09-25 01:33 by robert.rozee
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4036
Posted: 03:46pm 24 Sep 2024
Copy link to clipboard 
Print this post

  robert.rozee said  a simple approach would be to use a 'pre-processor'. as an example: ...


My Grandmother thanks you for your advice on the consumption of eggs .

That's a fallback position (Peter has already implemented some similar pre-processing in a less-systematic manner) but I think if we can crack this problem properly we open up the possibility of MMBasic programs being more aggressively tokenised and just maybe executing faster.

Best wishes,

Tom
Edited 2024-09-25 01:53 by thwill
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6768
Posted: 03:53pm 24 Sep 2024
Copy link to clipboard 
Print this post

OTOH how often do you *need* trimming a string to be fast? It's a string function and they are expected to be slower. Trimming a string is rarely going to be done 1000 times in a loop, it's more likely to be used for trimming a filename or user input or something like that. It's an ideal candidate for a user function.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2350
Posted: 04:05pm 24 Sep 2024
Copy link to clipboard 
Print this post

  thwill said  My Grandmother thanks you for your advice on the consumption of eggs .


your grandmother was clearly a wise woman  

rather than "a fallback position" we used to call it 'incremental improvement' back when i worked in R&D. i believe it worked well for the likes of Toyota  


cheers,
rob   :-)
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4036
Posted: 04:21pm 24 Sep 2024
Copy link to clipboard 
Print this post

  robert.rozee said  rather than "a fallback position" we used to call it 'incremental improvement' back when i worked in R&D. i believe it worked well for the likes of Toyota


But Peter (whom I hasten to add is not my grandmother ) has been pulling this trick / incremental improvement since the earliest CMM2 firmware I believe. It might be nice if we took a bit of a leap forward in this area, perhaps we can support 10,000+ function tokens rather than 128 so we never have to worry about it again ... at this point someone (Mick?) will argue (possibly correctly) that the token limit helped to stop the language from sprawling too badly .

Best wishes,

Tom
Edited 2024-09-25 02:23 by thwill
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6768
Posted: 04:37pm 24 Sep 2024
Copy link to clipboard 
Print this post

Of course, someone has to document all these new function tokens in the manual. I think Geoff might argue against adding too many. :)

FUNCTION / END FUNCTION is an elegant way to make the system do what you want. You only have the functions that you need without clogging up the flash area with more and having to find tokens for them. Ok, it's slower, but how important that is depends purely on your program. It might be easier to simply recode with more stuff in line rather than in SUBs and FUNCTIONs.

Every new command or function is another one for me to forget how to use. :(
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Print this page


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

© JAQ Software 2024