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

@ -1,24 +0,0 @@
/*
* Copyright (c) 2018 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: 2022-01-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.
*/
#pragma once
#include <maxbase/cdefs.h>
MXB_BEGIN_DECLS
/**
* Thread-safe strerror
*/
const char* mxb_strerror(int error);
MXB_END_DECLS

View File

@ -0,0 +1,146 @@
/*
* Copyright (c) 2018 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: 2022-01-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.
*/
#pragma once
#include <maxbase/ccdefs.hh>
#include <algorithm>
#include <string>
/**
* Thread-safe (but not re-entrant) strerror.
*
* @param error An errno value.
*
* @return The corresponding string.
*/
const char* mxb_strerror(int error);
namespace maxbase
{
/**
* Left trim a string.
*
* @param str String to trim.
* @return @c str
*
* @note If there is leading whitespace, the string is moved so that
* the returned pointer is always the same as the one given as
* argument.
*/
char* ltrim(char* str);
/**
* Right trim a string.
*
* @param str String to trim.
* @return @c str
*
* @note The returned pointer is always the same the one given as
* argument.
*/
char* rtrim(char* str);
/**
* Left and right trim a string.
*
* @param str String to trim.
* @return @c str
*
* @note If there is leading whitespace, the string is moved so that
* the returned pointer is always the same the one given as
* argument.
*/
char* trim(char* str);
/**
* @brief Left trim a string.
*
* @param s The string to be trimmed.
*/
inline void ltrim(std::string& s)
{
s.erase(s.begin(),
std::find_if(s.begin(),
s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
}
/**
* @brief Right trim a string.
*
* @param s The string to be trimmed.
*/
inline void rtrim(std::string& s)
{
s.erase(std::find_if(s.rbegin(),
s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(),
s.end());
}
/**
* @brief Trim a string.
*
* @param s The string to be trimmed.
*/
inline void trim(std::string& s)
{
ltrim(s);
rtrim(s);
}
/**
* @brief Left-trimmed copy of a string.
*
* @param s The string to the trimmed.
*
* @return A left-trimmed copy of the string.
*/
inline std::string ltrimmed_copy(const std::string& original)
{
std::string s(original);
ltrim(s);
return s;
}
/**
* @brief Right-trimmed copy of a string.
*
* @param s The string to the trimmed.
*
* @return A right-trimmed copy of the string.
*/
inline std::string rtrimmed_copy(const std::string& original)
{
std::string s(original);
rtrim(s);
return s;
}
/**
* @brief Trimmed copy of a string.
*
* @param s The string to the trimmed.
*
* @return A trimmed copy of the string.
*/
inline std::string trimmed_copy(const std::string& original)
{
std::string s(original);
ltrim(s);
rtrim(s);
return s;
}
}

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