Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 21:09 14 Nov 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: CSUB cLTrim and cRTrim

Author Message
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1676
Posted: 02:48pm 12 Sep 2020
Copy link to clipboard 
Print this post

Hi,
here are my CSUB versions of Ltrim() and Rtrim() for the CMM2.
After all, it's not as versatile as Geoff's useful code example (see manual), but it's significantly faster (~10us).





#include "ARMCFunctions.h"
#define NULL (void *) 0

void ltrim(char* string, char* b_char) {
// Note: doesn't work for string literals
// eg ltrim("   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];

// 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;
}


#include "ARMCFunctions.h"
#define NULL (void *) 0

void rtrim(char* string, char* b_char) {
unsigned char len = (unsigned char)*string;
unsigned char spaces = 0;
char* stemp = string;// copy string pointer
char bc = ' ';       // default blank char = ' '

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

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

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
*string = (char)len; // Store BASIC (trimmed) string length
return;
}


Demo file and source:
CSUB Trim v1.02.zip


You can use it as is or as a Function:
Function LTRIM(s$) as STRING
cLtrim(S$)
LTRIM=S$
End Function

a$=LTRIM("   Hello World")
? ">"a$"<"
>Hello World<


and you can eliminate (almost) any leading (or trailing) characters.

eg a$="0000000012345678", cLtrim(a$,"0"), Print a$ gives you "12345678"

Any feedback appreciated.

Regards
Michael

Edit:
CSUB Trim v1.01.zip replaced
TRIM (combines rTrim and lTrim) added
1.02 Demo corrected
Edited 2020-09-13 17:21 by twofingers
causality ≠ correlation ≠ coincidence
 
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