add a remove_white(...) method

This commit is contained in:
2024-08-06 20:13:31 +02:00
parent 6165a2ece3
commit 54b8ebfd8a
3 changed files with 41 additions and 4 deletions

View File

@@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2017-2022, Tomasz Sowa * Copyright (c) 2017-2024, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * 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);
}
} }

View File

@@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2017-2022, Tomasz Sowa * Copyright (c) 2017-2024, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * 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::string & str, bool check_carriage_return_too = true);
void trim_last_new_lines(std::wstring & 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);
} }

View File

@@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2021-2022, Tomasz Sowa * Copyright (c) 2021-2024, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -35,7 +35,6 @@
#ifndef headerfile_pikotools_src_convert_text_private #ifndef headerfile_pikotools_src_convert_text_private
#define headerfile_pikotools_src_convert_text_private #define headerfile_pikotools_src_convert_text_private
#include <string>
#include "text.h" #include "text.h"
@@ -401,6 +400,31 @@ void trim_generic(StringType & s, wchar_t c)
} }
template<typename StringType>
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 } // namespace pt_private