String routines to complement <string> && <cstring>:
Classes | Macros | Functions
string_tool.h File Reference

Rutinas de hileras que complementan <string> y <cstring>. More...

#include <list>
#include <string>
#include <cstring>

Go to the source code of this file.

Classes

struct  string_tool
 Rutinas string_tool.h de hileras que complementan <string> y <cstring>. More...
 

Macros

#define A_TILDE_64   "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"
 
#define A_SIMPLE_64   "AAAAAAECEEEEIIIIDNOOOOOx0UUUUYPsaaaaaaeceeeeiiiiOnooooo/0uuuuypy"
 

Functions

void tolower (std::string &line)
 Convierte a minúscula todas las letras de line. More...
 
void trimQuote (std::string &word)
 Elimina comillas ['] ["] del principio o final de 'word'. More...
 
bool isalpha (const std::string &line)
 Regresa true si todos los caracteres de line son alfabéticos. More...
 
bool is_blank (const std::string &word)
 Retorna true si todos las caracteres en word son blancos. More...
 
bool issuffix (const char *str, const char *suffix)
 Verifica si suffix es un sufijo de str. More...
 
unsigned digitCount (const std::string &str, unsigned i)
 Retorna la cantidad de dígitos consecutivos a partir de str[i]. More...
 
unsigned alphaCount (const std::string &str, unsigned i)
 Retorna la cantidad de letras consecutivas a partir de 'str[i]'. More...
 
unsigned number_suffix (const std::string &str)
 Retorna el número que está al final de str. More...
 
unsigned str2uint (const std::string &str)
 Convierte 's' un número entero sin signo. More...
 
std::string tostring (unsigned n, unsigned width)
 Retorna la hilera que contiene los últimos 'width' dígitos de 'N'. More...
 
void tostring (unsigned N, unsigned width, char *str)
 Almacena en 'str' los últimos 'width' dígitos de 'N'. More...
 
char * strdel (char *str, size_t from, size_t len)
 Deletes 'len' chars from 'str' starting at index 'from'. More...
 
char * strdel2 (char *str, size_t len)
 Deletes 'len' chars from the start of 'str'. More...
 
bool isdash (char c)
 Retorna true si es uno de { '-' '_' '~' }. More...
 
bool is_email (char ch)
 Retorna true si ch es un caracter son letras que pueden aparecer en una dirección email. More...
 
char removeAccented (char ch)
 Retorna la letra sin acento que corresponde a ch. More...
 
const char * removeAccented (char *str)
 Convierte todas las letras con acento en letras sin accento. More...
 
const std::string & removeAccented (std::string &str)
 Convierte todas las letras con acento en letras sin accento. More...
 
void tokens (const std::string &renglon, std::list< std::string > &L, const char *delimiters)
 Separa las palabras de 'renglon' y las deja en la lista 'L'. More...
 

Detailed Description

Rutinas de hileras que complementan <string> y <cstring>.

Author
Adolfo Di Mare adolf.nosp@m.o@di.nosp@m.-mare.nosp@m..com
Date
2012

Definition in file string_tool.h.

Macro Definition Documentation

◆ A_TILDE_64

#define A_TILDE_64   "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"

Definition at line 53 of file string_tool.h.

◆ A_SIMPLE_64

#define A_SIMPLE_64   "AAAAAAECEEEEIIIIDNOOOOOx0UUUUYPsaaaaaaeceeeeiiiiOnooooo/0uuuuypy"

Definition at line 55 of file string_tool.h.

Function Documentation

◆ tolower()

void tolower ( std::string &  line)

Convierte a minúscula todas las letras de line.

  • Solo funciona para letras ASCII.
{{ // test::tolower()
std::string A = "A"; tolower(A);
assertTrue( "a"==A );
std::string ABC123XYZ = "ABC123XYZ";
tolower( ABC123XYZ );
assertTrue( "abc123xyz" == ABC123XYZ );
}}

Definition at line 19 of file string_tool.cpp.

◆ trimQuote()

void trimQuote ( std::string &  word)

Elimina comillas ['] ["] del principio o final de 'word'.

Definition at line 121 of file string_tool.cpp.

◆ isalpha()

bool isalpha ( const std::string &  line)

Regresa true si todos los caracteres de line son alfabéticos.

{{ // test::isalpha()
assertTrue( isalpha("abcABCxyz") );
assertFalse( isalpha("123abc456") );
}}

Definition at line 140 of file string_tool.cpp.

◆ is_blank()

bool is_blank ( const std::string &  word)

Retorna true si todos las caracteres en word son blancos.

  • Usa la rutina estándar isspace() para determinar si un caracter es blanco.
{{ // test::is_blank()
assertTrue( is_blank(" \t\r\n\v") );
assertFalse( is_blank(" ? ") );
}}

Definition at line 157 of file string_tool.cpp.

◆ issuffix()

bool issuffix ( const char *  str,
const char *  suffix 
)

Verifica si suffix es un sufijo de str.

  • Ignora las diferencias entre mayúsculas y minúsculas.
{{ // test::issuffix()
assertTrue( issuffix("la mano alzada","ALZADA" ) );
assertFalse( issuffix("ALZADA","la mano alzada" ) );
assertTrue( issuffix("alzada","ALZADA" ) );
assertTrue( issuffix(0,"") );
assertTrue( issuffix("",0) ); // NULL
}}

Definition at line 172 of file string_tool.cpp.

◆ digitCount()

unsigned digitCount ( const std::string &  str,
unsigned  i 
)

Retorna la cantidad de dígitos consecutivos a partir de str[i].

  • Usa la rutina estándar isdigit().
  • Retorna 0 (cero) si no hay dígitos a partir de str[i].
  • Retorna string::npos si hay errores.
{{ // test::digitCount()
string sAE = "ABCDE";
string s15 = "12345";
assertTrue( digitCount("12345",0)==5 && s15[0]=='1' );
assertTrue( digitCount("12345",4)==1 && s15[4]=='5' );
assertTrue( digitCount("12345",5)==string::npos ); // fuera de rango
assertTrue( digitCount("ABCDE",0)==0 );
}}

Definition at line 209 of file string_tool.cpp.

◆ alphaCount()

unsigned alphaCount ( const std::string &  str,
unsigned  i 
)

Retorna la cantidad de letras consecutivas a partir de 'str[i]'.

  • Usa la rutina estándar isalpha().
  • Retorna 0 (cero) si no hay letras a partir de 'str[i]'.
  • Retorna string::npos si hay errores.
{{ // test::alphaCount()
assertTrue( alphaCount("ABCDE",0)==5 && "ABCDE"[0]=='A' );
assertTrue( alphaCount("ABCDE",4)==1 && "ABCDE"[4]=='E' );
assertTrue( alphaCount("ABCDE",5)==string::npos ); // fuera de rango
assertTrue( alphaCount("12345",0)==0 );
}}

Definition at line 229 of file string_tool.cpp.

◆ number_suffix()

unsigned number_suffix ( const std::string &  str)

Retorna el número que está al final de str.

  • Retorna std::string::npos si el sufijo de 'str' no es numérico.
  • Nunca retorna valores negativos (pues siempre ignora '-').
{{ // test::number_suffix()
assertTrue( 1==number_suffix("grupo001") );
assertTrue( 123==number_suffix("g123") );
assertTrue( 123==number_suffix("-123") );
assertTrue( std::string::npos==number_suffix("") );
assertTrue( std::string::npos==number_suffix("132xx") );
}}

Definition at line 248 of file string_tool.cpp.

◆ str2uint()

unsigned str2uint ( const std::string &  str)

Convierte 's' un número entero sin signo.

  • Solo convierte el prefijo numérico 'str'.
  • Ignora el signo si está presente.
{{ // test::str2uint()
assertTrue( 123 == str2uint("+123" ) );
assertTrue( 123 == str2uint("-123" ) );
assertTrue( 0 == str2uint("") );
assertTrue( 0 == str2uint("+-") );
assertTrue( 2012 == str2uint("2012.3.etc...") );
}}

Definition at line 274 of file string_tool.cpp.

◆ tostring() [1/2]

std::string tostring ( unsigned  N,
unsigned  width 
)

Retorna la hilera que contiene los últimos 'width' dígitos de 'N'.

  • Rellena con ceros al principio si hace falta.
{{ // test::tostring()
assertTrue( "001" == tostring(1, 3) );
assertTrue( "789" == tostring(123456789, 3) );
assertTrue( "" == tostring(123456789, 0) ); // 0==>""
}}

Definition at line 304 of file string_tool.cpp.

◆ tostring() [2/2]

void tostring ( unsigned  N,
unsigned  width,
char *  str 
)

Almacena en 'str' los últimos 'width' dígitos de 'N'.

  • Rellena con ceros al principio si hace falta.
  • Graba el fin de hilera '\0' en 'str[width]'.
{{ // test::tostring_char()
char touch[16]; assertTrue( !strcmp( "abc", "abc" ) );
tostring( 1, 3, touch ); assertTrue( !strcmp( "001", touch ) );
tostring(123456, 3, touch ); assertTrue( !strcmp( "456", touch ) );
tostring(123456, 0, touch ); assertTrue( !strcmp( "", touch ) );
}}

Definition at line 325 of file string_tool.cpp.

◆ strdel()

char* strdel ( char *  str,
size_t  from,
size_t  len 
)

Deletes 'len' chars from 'str' starting at index 'from'.

{{ // test::strdel()
char str[] = "12.==.789";
strdel( str, 2,4 );
assertTrue( 0==strcmp( str, "12789" ) );
strdel( str, strlen(str), 1 );
assertTrue( 0==strcmp( str, "12789" ) );
strdel( str, size_t(911), 1 );
assertTrue( 0==strcmp( str, "12789" ) );
}}

Definition at line 384 of file string_tool.cpp.

◆ strdel2()

char* strdel2 ( char *  str,
size_t  len 
)

Deletes 'len' chars from the start of 'str'.

{{ // test::strdel2()
char str[] = "12.==.789";
strdel2( str+2,4 );
assertTrue( 0==strcmp( str, "12789" ) );
strdel2( str+strlen(str), 1 );
assertTrue( 0==strcmp( str, "12789" ) );
strdel2( str+size_t(911), 1 );
assertTrue( 0==strcmp( str, "12789" ) );
}}

Definition at line 407 of file string_tool.cpp.

◆ isdash()

bool isdash ( char  c)
inline

Retorna true si es uno de { '-' '_' '~' }.

Definition at line 37 of file string_tool.h.

◆ is_email()

bool is_email ( char  ch)
inline

Retorna true si ch es un caracter son letras que pueden aparecer en una dirección email.

  • Es equivalente a ( isalpha(ch) || ch=='-' || ch=='.' || ch=='_' )
{{ // test::is_email()
const char* yo = "adolfo@di-mare.com";
std::string YO = yo;
size_t prev = YO.find('@');
for ( size_t i=0; i<prev; ++i ) {
assertTrue( is_email( YO[i] ) );
}
size_t post = prev+1;
for ( size_t i=post; i<YO.size(); ++i ) {
assertTrue( is_email( YO[i] ) );
}
}}

Definition at line 48 of file string_tool.h.

◆ removeAccented() [1/3]

char removeAccented ( char  ch)

Retorna la letra sin acento que corresponde a ch.

Definition at line 39 of file string_tool.cpp.

◆ removeAccented() [2/3]

const char* removeAccented ( char *  str)

Convierte todas las letras con acento en letras sin accento.

Definition at line 64 of file string_tool.cpp.

◆ removeAccented() [3/3]

const std::string& removeAccented ( std::string &  str)

Convierte todas las letras con acento en letras sin accento.

Definition at line 95 of file string_tool.cpp.

◆ tokens()

void tokens ( const std::string &  renglon,
std::list< std::string > &  L,
const char *  delimiters 
)

Separa las palabras de 'renglon' y las deja en la lista 'L'.

Usa los caracteres 'delimiters' como separadores. Se brinca los espacios en blanco. El contenido anterior del contenedor 'L' siempre queda eliminado.

Precondition
Siempre es necesario que el blanco ' ' aparezca como caracter delimitador en delimiters.
{{ // test::tokens()
std::string ln;
ln = " gsec =##-t|##-tpg (mejor peor); ;?-### ;etc." ;
// 1 2 34 5 6 7 8 910 11 12 13 14
#if 0
[gsec] [=] [##-t] [|] [##-tpg] [(] [mejor] [peor] [)] [;] [;] [?-###] [;] [etc.]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#endif
std::list< std::string > LLL;
std::list< std::string >::const_iterator it;
tokens( ln, LLL , " =();|" );
assertTrue( 14 == LLL.size() ); assertTrue( 12 != LLL.size() );
it = LLL.begin(); assertTrue( *it == "gsec" );
++it; assertTrue( *it == "=" );
++it; assertTrue( *it == "##-t" );
it = LLL.begin(); for (int i=1; i<6; ++i,it++) { } assertTrue( *it == "(" );
it = LLL.begin(); for (int i=1; i<10; ++i,it++) { } assertTrue( *it == ";" );
it = LLL.begin(); for (int i=1; i<12; ++i,it++) { } assertTrue( *it == "?-###" );
{ // puros blancos
std::list< std::string > LLL;
std::list< std::string >::const_iterator it;
std::string ln;
ln = " " ; tokens( ln, LLL , " ;" ); // puros blancos...
assertTrue( LLL.empty() );
ln = " ; etc "; tokens( ln, LLL , " ;" ); // puros blancos...
assertTrue( LLL.size()==2 && LLL.front()==";" );
}
}}

Definition at line 348 of file string_tool.cpp.

str2uint
unsigned str2uint(const std::string &str)
Convierte 's' un número entero sin signo.
Definition: string_tool.cpp:274
issuffix
bool issuffix(const char *str, const char *suffix)
Verifica si suffix es un sufijo de str.
Definition: string_tool.cpp:172
alphaCount
unsigned alphaCount(const std::string &str, unsigned i)
Retorna la cantidad de letras consecutivas a partir de 'str[i]'.
Definition: string_tool.cpp:229
is_blank
bool is_blank(const std::string &word)
Retorna true si todos las caracteres en word son blancos.
Definition: string_tool.cpp:157
A_TILDE_64
#define A_TILDE_64
Definition: string_tool.h:53
assertTrue
#define assertTrue(cond)
(cond ? () : cout << "cond" )
Definition: uUnit.h:93
number_suffix
unsigned number_suffix(const std::string &str)
Retorna el número que está al final de str.
Definition: string_tool.cpp:248
strdel2
char * strdel2(char *str, size_t len)
Deletes 'len' chars from the start of 'str'.
Definition: string_tool.cpp:407
tostring
std::string tostring(unsigned N, unsigned width)
Retorna la hilera que contiene los últimos 'width' dígitos de 'N'.
Definition: string_tool.cpp:304
tolower
void tolower(std::string &line)
Convierte a minúscula todas las letras de line.
Definition: string_tool.cpp:19
assertFalse
#define assertFalse(cond)
(!(cond) ? () : cout << "!" << (cond)" )
Definition: uUnit.h:95
removeAccented
char removeAccented(char ch)
Retorna la letra sin acento que corresponde a ch.
Definition: string_tool.cpp:39
isalpha
bool isalpha(const std::string &line)
Regresa true si todos los caracteres de line son alfabéticos.
Definition: string_tool.cpp:140
A_SIMPLE_64
#define A_SIMPLE_64
Definition: string_tool.h:55
is_email
bool is_email(char ch)
Retorna true si ch es un caracter son letras que pueden aparecer en una dirección email.
Definition: string_tool.h:48
tokens
void tokens(const std::string &renglon, std::list< std::string > &L, const char *delimiters)
Separa las palabras de 'renglon' y las deja en la lista 'L'.
Definition: string_tool.cpp:348
strdel
char * strdel(char *str, size_t from, size_t len)
Deletes 'len' chars from 'str' starting at index 'from'.
Definition: string_tool.cpp:384
digitCount
unsigned digitCount(const std::string &str, unsigned i)
Retorna la cantidad de dígitos consecutivos a partir de str[i].
Definition: string_tool.cpp:209