trim-newlines.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 #include <string>
7 
8 namespace utilities {
9 namespace string {
10 
11 
12 // Prepare a block of text for word-wrap by removing newlines:
13 // - Remove trailing spaces from lines
14 // - Multiple newlines stay as-is, so still separate paragraphs
15 // - Newlines preceded by colons stay
16 // - Newlines not preceded or followed by anything (beginning/end) stay
17 // - All other newlines replaced by spaces
18 // Example: (\n added just for verbosity)
19 // First line \n // extra space removed
20 // second line\n // joined into first line
21 // \n // stays
22 // third line: \n // trimmed; stays
23 // fourth\n // trailing \n stays
24 // Becomes:
25 // First line second line\n
26 // \n
27 // third line:\n
28 // fourth\n
30 {
31  char const * const base = text.c_str(); // first character in the text
32  char * dest = (char *)base; // running pointer to next destination character
33  char const * src = dest; // running pointer to current source character
34  // "abc \nline"
35  // dest-^ ^-src
36  while( *src )
37  {
38  // Copy everything until we hit a newline
39  if( *src != '\n' )
40  {
41  *dest++ = *src++;
42  continue;
43  }
44  // Go back and remove spaces
45  while( dest > base && dest[-1] == ' ' )
46  --dest;
47  // We're going to insert something (either space or newline); move src forward by one
48  ++src;
49  // If we're at the end of the string -- don't touch
50  // If we're at another newline -- don't touch
51  // If we're at the start of the text (dest==base) -- don't touch
52  if( *src && *src != '\n' && dest > base )
53  {
54  // If previous line does not end with ':' and is not empty, insert a space
55  if( dest[-1] != ':' && dest[-1] != '\n' )
56  {
57  *dest++ = ' ';
58  continue;
59  }
60  }
61  *dest++ = '\n';
62  }
63  text.resize( dest - base );
64  return text;
65 }
66 
67 
68 } // namespace string
69 } // namespace utilities
GLsizei const GLchar *const * string
GLenum src
Definition: glext.h:1751
std::string trim_newlines(std::string text)
Definition: trim-newlines.h:29
LZ4LIB_API char * dest
Definition: lz4.h:438


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:12