diff --git a/src/convert/text.cpp b/src/convert/text.cpp index 6eafd6e..69fd2f5 100644 --- a/src/convert/text.cpp +++ b/src/convert/text.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2017-2022, Tomasz Sowa + * Copyright (c) 2017-2024, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -544,6 +544,17 @@ void trim_last_new_lines(std::wstring & str, bool check_carriage_return_too) } +void remove_white(std::string & str, bool check_additional_chars, bool treat_new_line_as_white) +{ + pt_private::remove_white_generic(str, check_additional_chars, treat_new_line_as_white); +} + + +void remove_white(std::wstring & str, bool check_additional_chars, bool treat_new_line_as_white) +{ + pt_private::remove_white_generic(str, check_additional_chars, treat_new_line_as_white); +} + } diff --git a/src/convert/text.h b/src/convert/text.h index e93e195..b8b0244 100644 --- a/src/convert/text.h +++ b/src/convert/text.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2017-2022, Tomasz Sowa + * Copyright (c) 2017-2024, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -204,6 +204,8 @@ 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); +void remove_white(std::string & str, bool check_additional_chars = true, bool treat_new_line_as_white = true); +void remove_white(std::wstring & str, bool check_additional_chars = true, bool treat_new_line_as_white = true); } diff --git a/src/convert/text_private.h b/src/convert/text_private.h index dd91aa3..26affa1 100644 --- a/src/convert/text_private.h +++ b/src/convert/text_private.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2021-2022, Tomasz Sowa + * Copyright (c) 2021-2024, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,7 +35,6 @@ #ifndef headerfile_pikotools_src_convert_text_private #define headerfile_pikotools_src_convert_text_private -#include #include "text.h" @@ -401,6 +400,31 @@ void trim_generic(StringType & s, wchar_t c) } +template +void remove_white_generic(StringType & s, bool check_additional_chars, bool treat_new_line_as_white) +{ + size_t i = s.size(); + size_t white_len = 0; + + while( i-- > 0 ) + { + if( is_white(s[i], check_additional_chars, treat_new_line_as_white) ) + { + white_len += 1; + } + else + if( white_len > 0 ) + { + s.erase(i+1, white_len); + white_len = 0; + } + } + + if( white_len > 0 ) + { + s.erase(0, white_len); + } +} } // namespace pt_private