TinyMUSH 3.3
TinyMUSH Server
Loading...
Searching...
No Matches
Functions
stringutil.c File Reference

String utilities. More...

#include "config.h"
#include "constants.h"
#include "typedefs.h"
#include "macros.h"
#include "externs.h"
#include "prototypes.h"
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
Include dependency graph for stringutil.c:

Functions

char * ansiChar (int ch)
 Convert ansi character code (x?) to ansi sequence. More...
 
int ansiNum (int ch)
 Convert ansi character code (x?) to numeric values. More...
 
char ansiLetter (int num)
 Convert ansi numeric values to character code (x?). More...
 
char ansiMushCode (int num, bool bg)
 
int ansiBitsMask (int num)
 ANSI packed state definitions – number-to-bitmask translation table. More...
 
int ansiBits (int num)
 ANSI packed state definitions – number-to-bitvalue translation table. More...
 
char * level_ansi (const char *s, bool ansi, bool xterm, bool truecolors)
 Go to a string and convert ansi to the lowest supported level. More...
 
char * strip_ansi (const char *s)
 Return a new string with ansi escape codes removed. More...
 
char * strip_xterm (char *s)
 Return a new string with xterm color code removed. More...
 
char * strip_24bit (char *s)
 
int strip_ansi_len (const char *s)
 Count non-escape-code characters. More...
 
char * normal_to_white (const char *raw)
 This function implements the NOBLEED flag. More...
 
char * ansi_transition_esccode (int ansi_before, int ansi_after)
 Handle the transition between two ansi sequence. More...
 
char * ansi_transition_mushcode (int ansi_before, int ansi_after)
 Handle the transition between two ansi sequence of mushcode. More...
 
char * ansi_transition_letters (int ansi_before, int ansi_after)
 Handle the transition between two ansi sequence of mushcode. More...
 
int ansi_map_states (const char *s, int **m, char **p)
 Identify ansi state of every character in a string. More...
 
char * remap_colors (const char *s, int *cmap)
 Allow a change of the color sequences. More...
 
char * translate_string (char *str, int type)
 Convert raw ansi to mushcode or strip it. More...
 
int rgb2xterm (long rgb)
 Convert an RGB value to xterm value. More...
 
int str2xterm (char *str)
 Convert a value to xterm color. More...
 
char * upcasestr (char *s)
 Capitalizes an entire string. More...
 
char * munge_space (char *string)
 Compress multiple spaces into one and remove leading/trailing spaces. More...
 
char * trim_spaces (char *string)
 Remove leading and trailing spaces. More...
 
char * grabto (char **str, char targ)
 Return portion of a string up to the indicated character. More...
 
int string_compare (const char *s1, const char *s2)
 Compare two string. Treat multiple spaces as one. More...
 
int string_prefix (const char *string, const char *prefix)
 Compare a string with a prefix. More...
 
const char * string_match (const char *src, const char *sub)
 Compare a string with a substring, accepts only nonempty matches starting at the beginning of a word. More...
 
char * replace_string (const char *old, const char *new, const char *string)
 Replace all occurences of a substring with a new substring. More...
 
void edit_string (char *src, char **dst, char *from, char *to)
 Replace all occurences of a substring with a new substring. Sensitive about ANSI codes, and handles special ^ and $ cases. More...
 
int minmatch (char *str, char *target, int min)
 Find if a substring at the start of a string exist. More...
 
int matches_exit_from_list (char *exit_list, char *pattern)
 Find if pattern is found in the exit list. More...
 
char * ltos (long num)
 
char * repeatchar (int count, char ch)
 Return a string with 'count' number of 'ch' characters. More...
 
void skip_esccode (char **s)
 Move the pointer after an ansi escape sequence. More...
 
void copy_esccode (char **s, char **t)
 Copy the ansi sequence into another pointer. More...
 
void safe_copy_esccode (char **s, char *buff, char **bufc)
 Copy the ansi sequence into another pointer, moving bufc to the end of the receiving buffer and watching for overflow. More...
 
void track_ansi_letters (char *t, int *ansi_state)
 Convert mushcode to ansi state. More...
 

Detailed Description

String utilities.

Author
TinyMUSH development team (https://github.com/TinyMUSH)
Version
3.3
Date
2021-01-04

Function Documentation

◆ ansi_map_states()

int ansi_map_states ( const char *  s,
int **  m,
char **  p 
)

Identify ansi state of every character in a string.

It is the responsibility of the caller to free m with XFREE(m, "ansi_map_states_ansi_map") and p with XFREE(p, "ansi_map_states_stripped");

Parameters
sPointer to the string to be mapped.
mPointer to the ansi map to be build.
pPointer to the mapped string.
Returns
The number of items mapped.

◆ ansi_transition_esccode()

char * ansi_transition_esccode ( int  ansi_before,
int  ansi_after 
)

Handle the transition between two ansi sequence.

It is the responsibility of the caller to free the returning buffer with XFREE();

Parameters
ansi_beforeAnsi state before transition.
ansi_afteransi state after transition.
Returns
A pointer to an ansi sequence that will do the transition.

◆ ansi_transition_letters()

char * ansi_transition_letters ( int  ansi_before,
int  ansi_after 
)

Handle the transition between two ansi sequence of mushcode.

It is the responsibility of the caller to free the returning buffer with XFREE();

Parameters
ansi_beforeAnsi state before transition.
ansi_afteransi state after transition.
Returns
A pointer to the letters of mushcode that will do the transition.

◆ ansi_transition_mushcode()

char * ansi_transition_mushcode ( int  ansi_before,
int  ansi_after 
)

Handle the transition between two ansi sequence of mushcode.

It is the responsibility of the caller to free the returning buffer with XFREE();

Parameters
ansi_beforeAnsi state before transition.
ansi_afteransi state after transition.
Returns
A pointer to a mushcode sequence that will do the transition.

◆ ansiBits()

int ansiBits ( int  num)

ANSI packed state definitions – number-to-bitvalue translation table.

ANSI packed state definitions – number-to-bitvalue translation

Parameters
numANSI number
Returns
int ANSI bitvalue.

◆ ansiBitsMask()

int ansiBitsMask ( int  num)

ANSI packed state definitions – number-to-bitmask translation table.

The mask specifies the state bits that are altered by a particular ansi code. Bits are laid out as follows:

0x1000 – No ansi. Every valid ansi code clears this bit. 0x0800 – inverse 0x0400 – flash 0x0200 – underline 0x0100 – highlight 0x0080 – "use default bg", set by ansi normal, cleared by other bg's 0x0070 – three bits of bg color 0x0008 – "use default fg", set by ansi normal, cleared by other fg's 0x0007 – three bits of fg color

ANSI packed state definitions – number-to-bitmask translation

The mask specifies the state bits that are altered by a particular ansi code. Bits are laid out as follows:

0x2000 – Bright color flags 0x1000 – No ansi. Every valid ansi code clears this bit. 0x0800 – inverse 0x0400 – flash 0x0200 – underline 0x0100 – highlight 0x0080 – "use default bg", set by ansi normal, cleared by other bg's 0x0070 – three bits of bg color 0x0008 – "use default fg", set by ansi normal, cleared by other fg's 0x0007 – three bits of fg color

Parameters
numANSI number
Returns
int bitmask

◆ ansiChar()

char * ansiChar ( int  ch)

Convert ansi character code (x?) to ansi sequence.

Parameters
chCharacter to convert
Returns
char* Ansi sequence

◆ ansiLetter()

char ansiLetter ( int  num)

Convert ansi numeric values to character code (x?).

Parameters
numANSI number
Returns
char ANSI character

◆ ansiNum()

int ansiNum ( int  ch)

Convert ansi character code (x?) to numeric values.

Parameters
chANSI character
Returns
int ANSI numeric values

◆ copy_esccode()

void copy_esccode ( char **  s,
char **  t 
)

Copy the ansi sequence into another pointer.

Parameters
sPointer containing the ansi sequence.
tPointer who will receive the ansi sequence.

◆ edit_string()

void edit_string ( char *  src,
char **  dst,
char *  from,
char *  to 
)

Replace all occurences of a substring with a new substring. Sensitive about ANSI codes, and handles special ^ and $ cases.

Parameters
srcPointer to the original string.
dstPointer to the new string.
fromPointer to the string to be replaced
toPointer to the string to be replaced with.
Returns
None

◆ grabto()

char * grabto ( char **  str,
char  targ 
)

Return portion of a string up to the indicated character.

Note that str will be move to the position following the searched character. If you need the original string, save it before calling this function.

Parameters
strThe string you want to search
targThe caracter you want to search for,
Returns
The string up to the character you've search for.

◆ level_ansi()

char * level_ansi ( const char *  s,
bool  ansi,
bool  xterm,
bool  truecolors 
)

Go to a string and convert ansi to the lowest supported level.

Parameters
sString to convert
ansiPlayer support ansi
xtermPlayer support xterm colors
truecolorsPlayer system support true colors
Returns
char* Converted string

◆ matches_exit_from_list()

int matches_exit_from_list ( char *  exit_list,
char *  pattern 
)

Find if pattern is found in the exit list.

Parameters
exit_listPointer to the list of names of an exit
patternPointer to the pattern we are searching
Returns
1 if the pattern is in the list, 0 if not.

◆ minmatch()

int minmatch ( char *  str,
char *  target,
int  min 
)

Find if a substring at the start of a string exist.

At least MIN characters must match. This is how flags are match by the command queue.

Parameters
strPointer to the string that will be search.
targetPointer to the string being search.
minMinimum length of match.
Returns
1 if found, 0 if not.

◆ munge_space()

char * munge_space ( char *  string)

Compress multiple spaces into one and remove leading/trailing spaces.

It is the responsibility of the caller to free the returned buffer with XFREE().

Parameters
sThe string that need to be munge
Returns
The string munged

◆ normal_to_white()

char * normal_to_white ( const char *  raw)

This function implements the NOBLEED flag.

It is the responsibility of the caller to free the returned buffer with XFREE().

Parameters
rawPointer to the string who need to be ansi terminated.
Returns
A pointer to the string terminated.

◆ remap_colors()

char * remap_colors ( const char *  s,
int *  cmap 
)

Allow a change of the color sequences.

It is the responsibility of the caller to free the returned buffer with XFREE().

Parameters
sPointer to the string to be remap.
mPointer to the ansi color map to use.
Returns
Pointer to the remapped string.

◆ repeatchar()

char * repeatchar ( int  count,
char  ch 
)

Return a string with 'count' number of 'ch' characters.

It is the responsibility of the caller to free the resulting buffer.

Parameters
countLength of the string to build
chCharacter used to fill the string.
Returns
A Pointer to the new string.

◆ replace_string()

char * replace_string ( const char *  old,
const char *  new,
const char *  string 
)

Replace all occurences of a substring with a new substring.

Parameters
oldThe string you want to replace.
newThe string you want to replace with.
stringThe string that is modified.
Returns
A pointer to the modified string.

◆ rgb2xterm()

int rgb2xterm ( long  rgb)

Convert an RGB value to xterm value.

Parameters
rgbColor to convert to xterm value
Returns
The xterm value of the color.

◆ safe_copy_esccode()

void safe_copy_esccode ( char **  s,
char *  buff,
char **  bufc 
)

Copy the ansi sequence into another pointer, moving bufc to the end of the receiving buffer and watching for overflow.

Parameters
sPointer containing the ansi sequence.
buffPointer to the receiving buffer.
bufcPointer to where the data will be copied into the receiving buffer.

◆ skip_esccode()

void skip_esccode ( char **  s)

Move the pointer after an ansi escape sequence.

Parameters
sPointer that need to be modified.

◆ str2xterm()

int str2xterm ( char *  str)

Convert a value to xterm color.

Parameters
strA string representing the color to be convert into xterm value. The value can be express as hex (#rrggbb), decimal (r g b). a 24 bit integer value, or the actual xterm value.
Returns
The xterm value of the color.

◆ string_compare()

int string_compare ( const char *  s1,
const char *  s2 
)

Compare two string. Treat multiple spaces as one.

Parameters
s1The first string to compare
s2The second string to compare
Returns
0 if the string are different.

◆ string_match()

const char * string_match ( const char *  src,
const char *  sub 
)

Compare a string with a substring, accepts only nonempty matches starting at the beginning of a word.

Parameters
srcThe string you want to search
subThe search term.
Returns
The position of the search term. 0 if not found.

◆ string_prefix()

int string_prefix ( const char *  string,
const char *  prefix 
)

Compare a string with a prefix.

Parameters
stringThe string you want to search
prefixThe prefix you are searching for.
Returns
0 if the prefix isn't found.

◆ strip_ansi()

char * strip_ansi ( const char *  s)

Return a new string with ansi escape codes removed.

It is the responsibility of the caller to free the returned buffer with XFREE().

Parameters
sPointer to the original string
Returns
0 Pointer to the new string with ansi code removed.

◆ strip_ansi_len()

int strip_ansi_len ( const char *  s)

Count non-escape-code characters.

Parameters
sPointer to the string.
Returns
An integer representing the number of character in the string.

◆ strip_xterm()

char * strip_xterm ( char *  s)

Return a new string with xterm color code removed.

It is the responsibility of the caller to free the returned buffer with XFREE().

Parameters
sPointer to the original string
Returns
Pointer to the new string with ansi code removed.

It is the responsibility of the caller to free the returned buffer with XFREE().

Parameters
sPointer to the original string
Returns
Pointer to the new string with ansi code removed.

◆ track_ansi_letters()

void track_ansi_letters ( char *  t,
int *  ansi_state 
)

Convert mushcode to ansi state.

Parameters
tPointer containing the ansi sequence.
ansi_stateThe ansi state that need to be updated.

◆ translate_string()

char * translate_string ( char *  str,
int  type 
)

Convert raw ansi to mushcode or strip it.

It is the responsibility of the caller to free the returned buffer with XFREE().

Parameters
strPointer to the string to be translated
type1 = Convert to mushcode, 0 strip ansi.
Returns
Pointer to the translatted string.

◆ trim_spaces()

char * trim_spaces ( char *  string)

Remove leading and trailing spaces.

Parameters
sThe string that need to be trimmed.
Returns
The trimmed string.

◆ upcasestr()

char * upcasestr ( char *  s)

Capitalizes an entire string.

Parameters
sThe string that need to be capitalized.
Returns
The string capitalized.