pikotools/src/convert/text.h

213 lines
7.7 KiB
C++

/*
* This file is a part of PikoTools
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2017-2022, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_pikotools_src_convert_text
#define headerfile_pikotools_src_convert_text
#include <string>
namespace pt
{
bool is_white(wchar_t c, bool check_additional_chars = true, bool treat_new_line_as_white = true);
bool is_digit(wchar_t c, int base = 10, int * digit = 0);
// IMPROVEME add tests
template<typename HexStringPointerType, typename BytesStringType>
bool hex_string_pointer_to_bytes(const HexStringPointerType * hex_string, BytesStringType & bytes, bool clear_bytes = true)
{
bool converted_correctly = true;
size_t i = 0;
if( clear_bytes )
bytes.clear();
for( ; converted_correctly && hex_string[i] != 0 && hex_string[i+1] != 0 ; i += 2)
{
int c1, c2;
converted_correctly = converted_correctly && is_digit(hex_string[i], 16, &c1);
converted_correctly = converted_correctly && is_digit(hex_string[i+1], 16, &c2);
if( converted_correctly )
{
bytes += (char)(unsigned char)(((c1 << 4) | c2));
}
}
if( hex_string[i] != 0 )
{
// one digit has left
converted_correctly = false;
}
return converted_correctly;
}
// IMPROVEME add tests
template<typename HexStringType, typename BytesStringType>
bool hex_string_to_bytes(const HexStringType & hex_string, BytesStringType & bytes, bool clear_bytes = true)
{
return hex_string_pointer_to_bytes(hex_string.c_str(), bytes, clear_bytes);
}
const char * skip_white(const char * str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
const wchar_t * skip_white(const wchar_t * str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
/*
*
* str_end is pointing at the end of the string (the last item + one)
*
* return value is a pointer to the first white character after a non-white character at the end
* or to the last+one if there is no any white characters
*
*/
const char * skip_white_from_back(const char * str_begin, const char * str_end, bool check_additional_chars = true, bool treat_new_line_as_white = true);
const wchar_t * skip_white_from_back(const wchar_t * str_begin, const wchar_t * str_end, bool check_additional_chars = true, bool treat_new_line_as_white = true);
const char * skip_white_from_back(const char * str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
const wchar_t * skip_white_from_back(const wchar_t * str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
char to_lower(char c);
wchar_t to_lower(wchar_t c);
char to_upper(char c);
wchar_t to_upper(wchar_t c);
void to_lower_emplace(std::string & str);
void to_lower_emplace(std::wstring & str);
void to_upper_emplace(std::string & str);
void to_upper_emplace(std::wstring & str);
std::string to_lower(const std::string & str);
std::wstring to_lower(const std::wstring & str);
std::string to_upper(const std::string & str);
std::wstring to_upper(const std::wstring & str);
int compare(const char * str1, const char * str2);
int compare(const wchar_t * str1, const wchar_t * str2);
int compare(const std::string & str1, const std::string & str2);
int compare(const std::wstring & str1, const std::wstring & str2);
int compare(const char * str1_begin, const char * str1_end, const char * str2);
int compare(const wchar_t * str1_begin, const wchar_t * str1_end, const wchar_t * str2);
/*
* compare no case
*/
int compare_nc(const char * str1, const char * str2);
int compare_nc(const wchar_t * str1, const wchar_t * str2);
int compare_nc(const std::string & str1, const std::string & str2);
int compare_nc(const std::wstring & str1, const std::wstring & str2);
int compare_nc(const char * str1_begin, const char * str1_end, const char * str2);
int compare_nc(const wchar_t * str1_begin, const wchar_t * str1_end, const wchar_t * str2);
bool is_equal(const char * str1, const char * str2);
bool is_equal(const wchar_t * str1, const wchar_t * str2);
bool is_equal(const std::string & str1, const std::string & str2);
bool is_equal(const std::wstring & str1, const std::wstring & str2);
bool is_equal(const char * str1_begin, const char * str1_end, const char * str2);
bool is_equal(const wchar_t * str1_begin, const wchar_t * str1_end, const wchar_t * str2);
bool is_equal_nc(const char * str1, const char * str2);
bool is_equal_nc(const wchar_t * str1, const wchar_t * str2);
bool is_equal_nc(const std::string & str1, const std::string & str2);
bool is_equal_nc(const std::wstring & str1, const std::wstring & str2);
bool is_equal_nc(const char * str1_begin, const char * str1_end, const char * str2);
bool is_equal_nc(const wchar_t * str1_begin, const wchar_t * str1_end, const wchar_t * str2);
bool is_substr(const char * short_str, const char * long_str);
bool is_substr(const wchar_t * short_str, const wchar_t * long_str);
bool is_substr(const std::string & short_str, const std::string & long_str);
bool is_substr(const std::wstring & short_str, const std::wstring & long_str);
bool is_substr_nc(const char * short_str, const char * long_str);
bool is_substr_nc(const wchar_t * short_str, const wchar_t * long_str);
bool is_substr_nc(const std::string & short_str, const std::string & long_str);
bool is_substr_nc(const std::wstring & short_str, const std::wstring & long_str);
void trim_first_white(std::string & str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
void trim_first_white(std::wstring & str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
void trim_last_white(std::string & str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
void trim_last_white(std::wstring & str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
void trim_white(std::string & str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
void trim_white(std::wstring & str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
void trim_first(std::string & str, wchar_t c);
void trim_first(std::wstring & str, wchar_t c);
void trim_last(std::string & str, wchar_t c);
void trim_last(std::wstring & str, wchar_t c);
void trim(std::string & str, wchar_t c);
void trim(std::wstring & str, wchar_t c);
void trim_last_new_lines(std::string & str, bool check_carriage_return_too = true);
void trim_last_new_lines(std::wstring & str, bool check_carriage_return_too = true);
}
#endif