Skip to content

Commit

Permalink
add utilities::string::trim-newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Nov 4, 2020
1 parent b886c6c commit 5645dd2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
43 changes: 43 additions & 0 deletions common/utilities/string/trim-newlines.h
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
15 changes: 15 additions & 0 deletions unit-tests/utilities/string/common.h
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

31 changes: 31 additions & 0 deletions unit-tests/utilities/string/test-trim-newlines.cpp
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" );
}

0 comments on commit 5645dd2

Please sign in to comment.