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:
parent
60cbeaf287
commit
1b5b789342
@ -20,7 +20,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include <maxbase/log.h>
|
||||
#include <maxbase/string.h>
|
||||
#include <maxbase/string.hh>
|
||||
|
||||
MXS_BEGIN_DECLS
|
||||
|
||||
|
@ -117,41 +117,6 @@ void gw_sha1_2_str(const uint8_t* in, int in_len, const uint8_t* in2, int
|
||||
int gw_getsockerrno(int fd);
|
||||
char* create_hex_sha1_sha1_passwd(char* passwd);
|
||||
|
||||
/**
|
||||
* Trim leading whitespace from 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* trim_leading(char* str);
|
||||
|
||||
/**
|
||||
* Trim trailing whitespace from a string.
|
||||
*
|
||||
* @param str String to trim.
|
||||
* @return @c str
|
||||
*
|
||||
* @note The returned pointer is always the same the one given as
|
||||
* argument.
|
||||
*/
|
||||
char* trim_trailing(char* str);
|
||||
|
||||
/**
|
||||
* Trim leading and trailing whitespace from 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);
|
||||
|
||||
void replace_whitespace(char* str);
|
||||
char* squeeze_whitespace(char* str);
|
||||
bool strip_escape_chars(char*);
|
||||
|
@ -34,86 +34,6 @@
|
||||
namespace maxscale
|
||||
{
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tokenize a string
|
||||
*
|
||||
|
@ -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
|
146
maxutils/maxbase/include/maxbase/string.hh
Normal file
146
maxutils/maxbase/include/maxbase/string.hh
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@ -23,7 +23,7 @@
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
|
||||
#include <maxbase/string.h>
|
||||
#include <maxbase/string.hh>
|
||||
|
||||
/**
|
||||
* Error logging for the logger itself.
|
||||
|
@ -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
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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)
|
||||
|
131
maxutils/maxbase/src/test/test_string.cc
Normal file
131
maxutils/maxbase/src/test/test_string.cc
Normal 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;
|
||||
}
|
@ -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
|
||||
|
||||
|
@ -2854,7 +2854,7 @@ static void update_field_infos(parsing_info_t* pi,
|
||||
|
||||
char func_name[strlen(f) + 3 + 1]; // strlen(substring) - strlen(substr) from below.
|
||||
strcpy(func_name, f);
|
||||
trim(func_name); // Sometimes the embedded parser leaves leading and trailing whitespace.
|
||||
mxb::trim(func_name); // Sometimes the embedded parser leaves leading and trailing whitespace.
|
||||
|
||||
// Non native functions are surrounded by back-ticks, let's remove them.
|
||||
remove_surrounding_back_ticks(func_name);
|
||||
|
@ -4445,8 +4445,8 @@ static bool get_key_and_value(char* arg, const char** pkey, const char** pvalue)
|
||||
{
|
||||
*p = 0;
|
||||
|
||||
*pkey = trim(arg);
|
||||
*pvalue = trim(p + 1);
|
||||
*pkey = mxb::trim(arg);
|
||||
*pvalue = mxb::trim(p + 1);
|
||||
}
|
||||
|
||||
return p != NULL;
|
||||
|
@ -642,7 +642,7 @@ static void fix_section_name(char* section)
|
||||
void fix_object_name(char* name)
|
||||
{
|
||||
squeeze_whitespace(name);
|
||||
trim(name);
|
||||
mxb::trim(name);
|
||||
replace_whitespace(name);
|
||||
}
|
||||
|
||||
@ -4352,7 +4352,7 @@ int config_parse_server_list(const char* servers, char*** output_array)
|
||||
* be trimmed of whitespace. */
|
||||
char srv_list_tmp[strlen(servers) + 1];
|
||||
strcpy(srv_list_tmp, servers);
|
||||
trim(srv_list_tmp);
|
||||
mxb::trim(srv_list_tmp);
|
||||
|
||||
bool error = false;
|
||||
int output_ind = 0;
|
||||
@ -4796,8 +4796,8 @@ bool config_parse_disk_space_threshold(MxsDiskSpaceThreshold* pDisk_space_thresh
|
||||
string path = entry.substr(0, j);
|
||||
string tail = entry.substr(j + 1);
|
||||
|
||||
mxs::trim(path);
|
||||
mxs::trim(tail);
|
||||
mxb::trim(path);
|
||||
mxb::trim(tail);
|
||||
|
||||
if (!path.empty() && !tail.empty())
|
||||
{
|
||||
|
@ -1432,7 +1432,7 @@ static json_t* server_json_attributes(const SERVER* server)
|
||||
char timebuf[30];
|
||||
time_t tim = server->node_ts;
|
||||
asctime_r(localtime_r(&tim, &result), timebuf);
|
||||
trim(timebuf);
|
||||
mxb::trim(timebuf);
|
||||
|
||||
json_object_set_new(attr, "last_heartbeat", json_string(timebuf));
|
||||
}
|
||||
|
@ -1667,7 +1667,7 @@ json_t* service_attributes(const SERVICE* service)
|
||||
char timebuf[30];
|
||||
|
||||
asctime_r(localtime_r(&service->stats.started, &result), timebuf);
|
||||
trim(timebuf);
|
||||
mxb::trim(timebuf);
|
||||
|
||||
json_object_set_new(attr, "started", json_string(timebuf));
|
||||
json_object_set_new(attr, "total_connections", json_integer(service->stats.n_sessions));
|
||||
|
@ -778,7 +778,7 @@ json_t* session_json_data(const Session* session, const char* host)
|
||||
char buf[60];
|
||||
|
||||
asctime_r(localtime_r(&session->stats.connect, &result), buf);
|
||||
trim(buf);
|
||||
mxb::trim(buf);
|
||||
|
||||
json_object_set_new(attr, "connected", json_string(buf));
|
||||
|
||||
|
@ -23,102 +23,6 @@ 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 trim_leading_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_leading_testcases = sizeof(trim_leading_testcases) / sizeof(trim_leading_testcases[0]);
|
||||
|
||||
TRIM_TEST_CASE trim_trailing_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_trailing_testcases = sizeof(trim_trailing_testcases) / sizeof(trim_trailing_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, trim);
|
||||
}
|
||||
|
||||
int test_trim_leading()
|
||||
{
|
||||
cout << "trim_leading()" << endl;
|
||||
return test(trim_leading_testcases, n_trim_leading_testcases, trim_leading);
|
||||
}
|
||||
|
||||
int test_trim_trailing()
|
||||
{
|
||||
cout << "trim_trailing()" << endl;
|
||||
return test(trim_trailing_testcases, n_trim_trailing_testcases, trim_trailing);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int test_checksums()
|
||||
{
|
||||
@ -169,13 +73,12 @@ int test_checksums()
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int rv = 0;
|
||||
|
||||
rv += test_trim();
|
||||
rv += test_trim_leading();
|
||||
rv += test_trim_trailing();
|
||||
rv += test_checksums<mxs::SHA1Checksum>();
|
||||
rv += test_checksums<mxs::CRC32Checksum>();
|
||||
|
||||
|
@ -459,45 +459,6 @@ bool mxs_mkdir_all(const char* path, int mask)
|
||||
return mkdir_all_internal(local_path, (mode_t)mask);
|
||||
}
|
||||
|
||||
char* trim_leading(char* str)
|
||||
{
|
||||
char* ptr = str;
|
||||
|
||||
while (isspace(*ptr))
|
||||
{
|
||||
ptr++;
|
||||
}
|
||||
|
||||
if (ptr != str)
|
||||
{
|
||||
memmove(str, ptr, strlen(ptr) + 1);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
char* trim_trailing(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 trim_leading(trim_trailing(str));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Replace whitespace with hyphens
|
||||
*
|
||||
@ -1229,42 +1190,4 @@ uint8_t* set_byteN(uint8_t* ptr, uint64_t value, int bytes)
|
||||
return ptr + bytes;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
size_t write_callback(char* ptr, size_t size, size_t nmemb, void* userdata)
|
||||
{
|
||||
std::string* buf = static_cast<std::string*>(userdata);
|
||||
|
||||
if (nmemb > 0)
|
||||
{
|
||||
buf->append(ptr, nmemb);
|
||||
}
|
||||
|
||||
return nmemb;
|
||||
}
|
||||
|
||||
size_t header_callback(char* ptr, size_t size, size_t nmemb, void* userdata)
|
||||
{
|
||||
std::unordered_map<std::string, std::string>* map =
|
||||
static_cast<std::unordered_map<std::string, std::string>*>(userdata);
|
||||
|
||||
if (nmemb > 0)
|
||||
{
|
||||
std::string data(ptr, size* nmemb);
|
||||
auto pos = data.find_first_of(':');
|
||||
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
std::string key = data.substr(0, pos);
|
||||
std::string value = data.substr(pos + 1);
|
||||
trim(key);
|
||||
trim(value);
|
||||
map->insert(std::make_pair(key, value));
|
||||
}
|
||||
}
|
||||
|
||||
return nmemb * size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1781,11 +1781,11 @@ json_t* Dbfw::diagnostics_json() const
|
||||
}
|
||||
|
||||
extern "C"
|
||||
void dbfilter_log_warning(const char* file, int line, const char* function,
|
||||
const char* format, const char* what)
|
||||
void log_warning(const char* module, const char* file, int line, const char* function,
|
||||
const char* format, const char* what)
|
||||
{
|
||||
char buffer[strlen(format) + strlen(what) + 1];
|
||||
|
||||
sprintf(buffer, format, what);
|
||||
mxb_log_message(LOG_WARNING, file, line, function, "%s", buffer);
|
||||
mxb_log_message(LOG_WARNING, module, file, line, function, "%s", buffer);
|
||||
}
|
||||
|
@ -905,7 +905,7 @@ void RegexHintFilter::set_source_addresses(const std::string& input_host_names,
|
||||
|
||||
for (auto host : mxs::strtok(host_names, ","))
|
||||
{
|
||||
char* trimmed_host = trim((char*)host.c_str());
|
||||
char* trimmed_host = mxb::trim((char*)host.c_str());
|
||||
|
||||
if (!add_source_address(trimmed_host, source_hosts))
|
||||
{
|
||||
|
@ -267,7 +267,7 @@ int avro_client_callback(DCB* dcb, DCB_REASON reason, void* userdata)
|
||||
*/
|
||||
std::pair<std::string, std::string> get_avrofile_and_gtid(std::string file)
|
||||
{
|
||||
mxs::ltrim(file);
|
||||
mxb::ltrim(file);
|
||||
auto pos = file.find_first_of(' ');
|
||||
std::string filename;
|
||||
std::string gtid;
|
||||
|
@ -2278,7 +2278,7 @@ static json_t* diagnostics_json(const MXS_ROUTER* router)
|
||||
|
||||
localtime_r(&session_last_event, &tm);
|
||||
asctime_r(&tm, buf);
|
||||
trim(buf);
|
||||
mxb::trim(buf);
|
||||
json_object_set_new(rval, "last_binlog_event_timestamp", json_string(buf));
|
||||
json_object_set_new(rval, "seconds_behind_master", json_integer(seconds_behind));
|
||||
}
|
||||
|
@ -4499,7 +4499,7 @@ static int blr_apply_change_master(ROUTER_INSTANCE* router,
|
||||
*/
|
||||
static char* get_connection_name(char* command, std::string* pConnection_name)
|
||||
{
|
||||
command = trim_leading(command);
|
||||
command = mxb::ltrim(command);
|
||||
|
||||
char* to = strcasestr(command, "TO");
|
||||
|
||||
|
@ -2113,7 +2113,7 @@ int execute_cmd(CLI_SESSION* cli)
|
||||
bool in_space = false;
|
||||
int nskip = 0;
|
||||
|
||||
args[0] = trim_leading(cli->cmdbuf);
|
||||
args[0] = mxb::ltrim(cli->cmdbuf);
|
||||
ptr = args[0];
|
||||
lptr = ptr;
|
||||
i = 1;
|
||||
|
Loading…
x
Reference in New Issue
Block a user