|
Forum Index : Microcontroller and PC projects : CMM2: CSUB cLTrim and cRTrim
| Author | Message | ||||
| twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1769 |
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 ‚My name is Ozymandias, king of kings Look on my works, ye Mighty, and despair!‘ Nothing beside remains. Round the decay Of that colossal wreck, boundless and bare The lone and ... |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |