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) 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 <string>
#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