1 /* String wrapping helper function.
2 Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3 Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4
5 This file is part of the Parma Polyhedra Library (PPL).
6
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23
24 #include "ppl-config.h"
25 #include "wrap_string.hh"
26 #include "assertions.hh"
27 #include <cstdlib>
28
29 namespace Parma_Polyhedra_Library {
30
31 namespace IO_Operators {
32
33 std::string
wrap_string(const std::string & src_string,const unsigned indent_depth,const unsigned preferred_first_line_length,const unsigned preferred_line_length)34 wrap_string(const std::string& src_string,
35 const unsigned indent_depth,
36 const unsigned preferred_first_line_length,
37 const unsigned preferred_line_length) {
38 const unsigned npos = C_Integer<unsigned>::max;
39 std::string dst_string;
40 const char *src = src_string.c_str();
41 for (unsigned line = 0; ; ++line) {
42 const unsigned line_length = ((line == 0)
43 ? preferred_first_line_length
44 : preferred_line_length);
45 unsigned last_comma = npos;
46 unsigned last_space = npos;
47 unsigned split_pos = npos;
48 unsigned idx;
49 for (idx = 0; idx <= line_length; ++idx) {
50 if (src[idx] == '\0' || src[idx] == '\n') {
51 split_pos = idx;
52 break;
53 }
54 if (src[idx] == ',' && idx < line_length) {
55 last_comma = idx;
56 }
57 if (is_space(src[idx]) && (idx == 0 || !is_space(src[idx-1]))) {
58 last_space = idx;
59 }
60 }
61 if (split_pos == npos) {
62 if (last_comma != npos) {
63 split_pos = last_comma + 1;
64 }
65 else if (last_space != npos) {
66 split_pos = last_space;
67 }
68 else {
69 for ( ; src[idx] != '\0'; ++idx) {
70 if (src[idx] == ',') {
71 ++idx;
72 break;
73 }
74 if (is_space(src[idx])) {
75 break;
76 }
77 }
78 split_pos = idx;
79 }
80 }
81 PPL_ASSERT(split_pos != npos);
82 if (split_pos > 0 && line > 0 && indent_depth > 0) {
83 dst_string.append(indent_depth, ' ');
84 }
85 dst_string.append(src, split_pos);
86 src += split_pos;
87 if (is_space(*src)) {
88 ++src;
89 }
90 while (*src == ' ') {
91 ++src;
92 }
93 if (*src == '\0') {
94 break;
95 }
96 dst_string.push_back('\n');
97 }
98 return dst_string;
99 }
100
101 } // namespace IO_Operators
102
103 } // namespace Parma_Polyhedra_Library
104
105