MXS-2208 Move trim-functions from maxscale to maxbase

log.h now includes string.hh, which is conceptually wrong, but
log.h will shortly disappear and be superceded by log.hh.
This commit is contained in:
Johan Wikman
2018-12-05 14:54:41 +02:00
parent 60cbeaf287
commit 1b5b789342
25 changed files with 352 additions and 339 deletions

View File

@ -23,7 +23,7 @@
#include <cstdio>
#include <ctime>
#include <maxbase/string.h>
#include <maxbase/string.hh>
/**
* Error logging for the logger itself.

View File

@ -19,7 +19,7 @@
#include <fstream>
#include <maxbase/assert.h>
#include <maxbase/log.h>
#include <maxbase/string.h>
#include <maxbase/string.hh>
#include <maxbase/worker.hh>
namespace

View File

@ -11,14 +11,15 @@
* Public License.
*/
#include <maxbase/string.h>
#include <maxbase/string.hh>
#include <ctype.h>
#include <string.h>
namespace
{
thread_local char errbuf[512]; // Enough for all errors
}
const char* mxb_strerror(int error)
@ -30,3 +31,47 @@ const char* mxb_strerror(int error)
return errbuf;
#endif
}
namespace maxbase
{
char* ltrim(char* str)
{
char* ptr = str;
while (isspace(*ptr))
{
ptr++;
}
if (ptr != str)
{
memmove(str, ptr, strlen(ptr) + 1);
}
return str;
}
char* rtrim(char* str)
{
char* ptr = strchr(str, '\0') - 1;
while (ptr > str && isspace(*ptr))
{
ptr--;
}
if (isspace(*(ptr + 1)))
{
*(ptr + 1) = '\0';
}
return str;
}
char* trim(char* str)
{
return ltrim(rtrim(str));
}
}

View File

@ -10,6 +10,10 @@ add_executable(test_semaphore test_semaphore.cc)
target_link_libraries(test_semaphore maxbase pthread rt)
add_test(test_semaphore test_semaphore)
add_executable(test_mxb_string test_string.cc)
target_link_libraries(test_mxb_string maxbase)
add_test(test_semaphore test_semaphore)
add_executable(test_worker test_worker.cc)
target_link_libraries(test_worker maxbase pthread rt)
add_test(test_worker test_worker)

View File

@ -0,0 +1,131 @@
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2019-07-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <maxbase/string.hh>
#include <maxbase/assert.h>
#include <string.h>
#include <iostream>
using std::cout;
using std::endl;
namespace
{
#define TRIM_TCE(zFrom, zTo) {zFrom, zTo}
struct TRIM_TEST_CASE
{
const char* zFrom;
const char* zTo;
};
TRIM_TEST_CASE trim_testcases[] =
{
TRIM_TCE("", ""),
TRIM_TCE("a", "a"),
TRIM_TCE(" a", "a"),
TRIM_TCE("a ", "a"),
TRIM_TCE(" a ", "a"),
TRIM_TCE(" a", "a"),
TRIM_TCE("a ", "a"),
TRIM_TCE(" a ", "a"),
TRIM_TCE(" a b ", "a b"),
};
const int n_trim_testcases = sizeof(trim_testcases) / sizeof(trim_testcases[0]);
TRIM_TEST_CASE ltrim_testcases[] =
{
TRIM_TCE("", ""),
TRIM_TCE("a", "a"),
TRIM_TCE(" a", "a"),
TRIM_TCE("a ", "a "),
TRIM_TCE(" a ", "a "),
TRIM_TCE(" a", "a"),
TRIM_TCE("a ", "a "),
TRIM_TCE(" a ", "a "),
TRIM_TCE(" a b ", "a b "),
};
const int n_ltrim_testcases = sizeof(ltrim_testcases) / sizeof(ltrim_testcases[0]);
TRIM_TEST_CASE rtrim_testcases[] =
{
TRIM_TCE("", ""),
TRIM_TCE("a", "a"),
TRIM_TCE(" a", " a"),
TRIM_TCE("a ", "a"),
TRIM_TCE(" a ", " a"),
TRIM_TCE(" a", " a"),
TRIM_TCE("a ", "a"),
TRIM_TCE(" a ", " a"),
TRIM_TCE(" a b ", " a b"),
};
const int n_rtrim_testcases = sizeof(rtrim_testcases) / sizeof(rtrim_testcases[0]);
int test(TRIM_TEST_CASE* pTest_cases, int n_test_cases, char* (*p)(char*))
{
int rv = 0;
for (int i = 0; i < n_test_cases; ++i)
{
const char* zFrom = pTest_cases[i].zFrom;
const char* zTo = pTest_cases[i].zTo;
char copy[strlen(zFrom) + 1];
strcpy(copy, zFrom);
char* z = p(copy);
if (strcmp(z, zTo) != 0)
{
++rv;
}
}
return rv;
}
int test_trim()
{
cout << "trim()" << endl;
return test(trim_testcases, n_trim_testcases, mxb::trim);
}
int test_ltrim()
{
cout << "ltrim()" << endl;
return test(ltrim_testcases, n_ltrim_testcases, mxb::ltrim);
}
int test_rtrim()
{
cout << "rtrim()" << endl;
return test(rtrim_testcases, n_rtrim_testcases, mxb::rtrim);
}
}
int main(int argc, char* argv[])
{
int rv = 0;
rv += test_trim();
rv += test_ltrim();
rv += test_rtrim();
return rv;
}

View File

@ -25,7 +25,7 @@
#include <maxbase/assert.h>
#include <maxbase/atomic.hh>
#include <maxbase/log.h>
#include <maxbase/string.h>
#include <maxbase/string.hh>
#define WORKER_ABSENT_ID -1