-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add utilities::string::trim-newlines
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2020 Intel Corporation. All Rights Reserved. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace utilities { | ||
namespace string { | ||
|
||
|
||
inline std::string trim_newlines( std::string s ) | ||
{ | ||
char const * const base = s.c_str(); | ||
char * dest = (char *)base; | ||
char const * src = dest; | ||
while( *src ) | ||
{ | ||
if( *src != '\n' ) | ||
{ | ||
*dest++ = *src++; | ||
continue; | ||
} | ||
while( dest > base && dest[-1] == ' ' ) | ||
--dest; | ||
++src; | ||
if( *src && *src != '\n' && dest > base ) | ||
{ | ||
if( dest[-1] != ':' && dest[-1] != '\n' ) | ||
{ | ||
*dest++ = ' '; | ||
continue; | ||
} | ||
} | ||
*dest++ = '\n'; | ||
} | ||
s.resize( dest - base ); | ||
return s; | ||
} | ||
|
||
|
||
} // namespace string | ||
} // namespace utilities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2020 Intel Corporation. All Rights Reserved. | ||
|
||
#define CATCH_CONFIG_MAIN | ||
|
||
#include "../../catch.h" | ||
|
||
#include <easylogging++.h> | ||
#ifdef BUILD_SHARED_LIBS | ||
// With static linkage, ELPP is initialized by librealsense, so doing it here will | ||
// create errors. When we're using the shared .so/.dll, the two are separate and we have | ||
// to initialize ours if we want to use the APIs! | ||
INITIALIZE_EASYLOGGINGPP | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2020 Intel Corporation. All Rights Reserved. | ||
|
||
#include "common.h" | ||
#include "../common/utilities/string/trim-newlines.h" | ||
|
||
using namespace utilities::string; | ||
|
||
TEST_CASE( "trim-newlines", "[string]" ) | ||
{ | ||
// No changes because nothing behind | ||
CHECK( trim_newlines( "" ) == "" ); | ||
CHECK( trim_newlines( "\n" ) == "\n" ); | ||
|
||
// Put something behind... | ||
CHECK( trim_newlines( "a" ) == "a" ); | ||
CHECK( trim_newlines( "\na" ) == "\na" ); | ||
CHECK( trim_newlines( " \n " ) == "\n " ); | ||
CHECK( trim_newlines( "a\n" ) == "a\n" ); // nothing in front | ||
CHECK( trim_newlines( "a\nb" ) == "a b" ); | ||
CHECK( trim_newlines( "a \nb" ) == "a b" ); // remove spaces at EOL | ||
|
||
// Multiple NLs should not get removed | ||
CHECK( trim_newlines( "a\n\nb" ) == "a\n\nb" ); | ||
CHECK( trim_newlines( "a\n\n \nb" ) == "a\n\n\nb" ); | ||
|
||
// Colons followed by NL should stay | ||
CHECK( trim_newlines( "line 1.\nline 2" ) == "line 1. line 2" ); | ||
CHECK( trim_newlines( "line 1:\nline 2" ) == "line 1:\nline 2" ); | ||
CHECK( trim_newlines( "line 1: \nline 2" ) == "line 1:\nline 2" ); | ||
} |