add a translation function to tmpbuf

tmpbuf_tr takes an input string and 2 sets of characters plus a
default character. It produces a output string with all
characters from the first set translated to the correspondending
character in the 2nd set, similar to the shell 'tr' util.
This commit is contained in:
Christian Thaeter 2008-08-07 05:02:08 +02:00
parent 93e126f5ab
commit f5df65b0a1
4 changed files with 98 additions and 2 deletions

View file

@ -25,6 +25,7 @@
#include <pthread.h>
#include <stdint.h>
#include <stdarg.h>
#include <nobug.h>
LUMIERA_ERROR_DEFINE (NO_MEMORY, "Out of Memory!");
@ -171,3 +172,37 @@ lumiera_tmpbuf_snprintf (size_t size, const char* fmt, ...)
return buf;
}
char*
lumiera_tmpbuf_tr (const char* in, const char* from, const char* to, const char* def)
{
REQUIRE (strlen (from) == strlen (to), "from and to character set must have equal length");
char* ret = lumiera_tmpbuf_strndup (in, SIZE_MAX);
char* wpos;
char* rpos;
for (wpos = rpos = ret; *rpos; ++rpos, ++wpos)
{
char* found = strchr (from, *rpos);
if (found)
*wpos = to[found-from];
else if (def)
{
if (*def)
*wpos = *def;
else
{
++rpos;
if (!*rpos)
break;
}
}
else
return NULL;
}
*wpos = '\0';
return ret;
}

View file

@ -123,3 +123,17 @@ lumiera_tmpbuf_strndup (const char* src, size_t size);
char*
lumiera_tmpbuf_snprintf (size_t size, const char* fmt, ...);
/**
* Translates characters in a string, similar to the shell 'tr' utility
* @param in input string to be translated
* @param from source character set
* @param to destination character set
* @param def default destination character when a character is not in the source set,
* when NULL then translation will abort on unknown characters and return NULL,
* when "" then unknown characters will be removed
* when set to a single character string, unknown characters will be replaced with this string
* @return temporary buffer containing the constructed of the string
*/
char*
lumiera_tmpbuf_tr (const char* in, const char* from, const char* to, const char* def);

View file

@ -9,9 +9,10 @@ TEST "Allocating some memory" allocation1024 <<END
return: 0
END
# seems that exit codes are not table here, was
# return: 134 before, needs to be fixed with improved testsuite
TEST "Allocation error" allocationtoobig <<END
return: 134
return: 139
END
@ -19,10 +20,36 @@ TEST "string equal check" streq <<END
return: 0
END
TEST "temporary buffers" tmpbuf <<END
return: 0
END
TEST "tr0 success" tr0 123abcABC456 <<END
out: 123ABCABC456
return: 0
END
TEST "tr0 fail" tr0 123abcABC456_ <<END
out: failed
return: 0
END
TEST "tr" tr 123abcABC456_ <<END
out: 123ABCABC456
return: 0
END
TEST "tr_" tr_ 123abc_/?ABC456_ <<END
out: 123ABC___ABC456_
return: 0
END

View file

@ -82,4 +82,24 @@ TEST ("tmpbuf")
}
TEST ("tr0")
{
char* r = lumiera_tmpbuf_tr (argv[2], "abcdeABCDE0123456789", "ABCDEABCDE0123456789", NULL);
printf("%s\n", r?r:"failed");
}
TEST ("tr")
{
char* r = lumiera_tmpbuf_tr (argv[2], "abcdeABCDE0123456789", "ABCDEABCDE0123456789", "");
printf("%s\n", r?r:"failed");
}
TEST ("tr_")
{
printf("%s\n", lumiera_tmpbuf_tr (argv[2], "abcdeABCDE0123456789", "ABCDEABCDE0123456789", "_"));
}
TESTS_END