// From QDevels, posted by outlawvoid convert_string(char *src, char start, char end, char add, char *dest){ int n = -1; while ((dest[++n] = src[n])) if ((dest[n] >= start) && (dest[n] <= end) && (dest[n] != '\n')) dest[n] += add;}/* Examples of convert_string usage { char text[] = "abcdefgABCDEFG1234567\n\0"; convert_string(text, 'a', 'z', ('A'-'a'), text); // a -> A my_bprintf (PRINT_CHAT, "text = %s\n", text); convert_string(text, 'A', 'Z', ('a'-'A'), text); // A -> a my_bprintf (PRINT_CHAT, "text = %s\n", text); convert_string(text, 0, 127, 128, text); // white -> green my_bprintf (PRINT_CHAT, "text = %s\n", text); convert_string(text, 128, 255, -128, text); // green -> white my_bprintf (PRINT_CHAT, "text = %s\n", text); } */
/** Replace characters in destination string. Parameter 'add' is added to each character found in source and result is placed in dest. Parameters 'start' and 'end' specify character range to replace. Source text must be a valid C string.*///QwazyWabbit// A pointer version to eliminate undefined behavior.void convert_string(char *src, char start, char end, char add, char *dest){ while ((*dest = *src)) { if ((*dest >= start) && (*dest <= end) && (*dest != '\n')) *dest += add; src++, dest++; }}
/** Set msb in specified string characters, copying them to destination. Text must be a valid C string. Source and destination can be the same. If dest == NULL the action occurs in-place. */void highlight_text (char *src, char *dest){ if (dest == NULL) dest = src; convert_string(src, 0, 127, 128, dest); // white -> green}/** Clear msb in specified string characters, copying them to destination. Text must be a valid C string. Source and destination can be the same. If dest == NULL the action occurs in-place. */void white_text (char *src, char *dest){ if (dest == NULL) dest = src; convert_string(src, 128, 255, -128, dest); // green -> white}/** Make text uppercase. Text must be a valid C string. Source and destination can be the same. If dest == NULL the action occurs in-place. */void toupper_text(char *src, char *dest){ if (dest == NULL) dest = src; convert_string(src, 'a', 'z', ('A'-'a'), dest); // a -> A}/** Make text lowercase. Text must be a valid C string. Source and destination can be the same string. If dest == NULL the action occurs in-place. */void tolower_text(char *src, char *dest){ if (dest == NULL) dest = src; convert_string(src, 'A', 'Z', ('a'-'A'), dest); // A -> a}
#include <stdio.h>int main(int argc, const char * argv[]){ char otext[] = "abcdefgABCDEFG1234567"; char text[] = "abcdefgABCDEFG1234567\n"; char target[sizeof text]; toupper_text(otext, target); // a -> A printf ("otext = %s\n", otext); printf ("target = %s\n", target); toupper_text(text, NULL); // a -> A printf ("text = %s\n", text); tolower_text(otext, target); // A -> a printf ("otext = %s\n", otext); printf ("target = %s\n", target); tolower_text(text, NULL); // A -> a printf ("text = %s\n", text); highlight_text(otext, target); // white -> green printf ("otext = %s\n", otext); printf ("target = %s\n", target); highlight_text(text, NULL); // white -> green printf ("text = %s\n", text); white_text(target, otext); // green -> white printf ("target = %s\n", target); printf ("otext = %s\n", otext); white_text(text, NULL); // green -> white printf ("text = %s\n", text); return 0;}