1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 * @file StringUtilities.h
24 * This file contains the String class.
33 * This class provides a set of utility functions that operate on strings.
35 class String
: public Singleton
<String
>
39 * Returns a vector of strings splitting the input at the specified separator.
41 * @param input The input to search for the separator in.
42 * @param separator The separator to serach for.
43 * @return The input, split into lines at each separator.
45 Strings
lines(const std::string
& input
, const char* separator
= "\n");
48 * Returns a string with the input strings concatenated.
50 * @param input The input to put together.
51 * @param filler The character to put between characters parts of the input.
52 * @param newlineat After how many lines of the input a newline will be added, when 0 no extra newlines will be added.
54 std::string
unlines(const Strings
& input
, const char* filler
, int newlineat
= 1);
57 * Convert all '\n' into in the input to new lines.
59 * @param input The input to search for lines containing '\n'.
60 * @return The output in which lines containing '\n' are split.
62 Strings
unlines(const Strings
& input
);
64 /** Find the longest line in the input and return it's lenght. */
65 size_t maxlength(const Strings
& content
);
68 * Draw an ASCII art box around the content using the specified header.
70 * @param content The lines that should be contained inside the box.
71 * @param header The header that will be put above the result (centered), will be left out if none is specified.
72 * @return One string that contains the 'boxed' content.
74 std::string
box(const Strings
& content
, const std::string
& header
= Global::Get()->EmptyString
);
77 * Creates a ASCII table out of the content using the specified header.
79 * @param content The 'body' of the table, is asserted to be of same 'length' as the header.
80 * @param header The header to use for the table.
81 * @return A set of strings that contain the table, each one row of the table.
83 Strings
createTable(const std::vector
<Strings
>& content
, const Strings
& header
);
85 /** Returns a string representation of an integer. */
86 std::string
fromInt(value_type value
);
88 /** Returns a string representation of an integer. */
89 std::string
fromLong(signed long value
);
91 /** Converts a string to upper case. */
92 std::string
toupper(const std::string
& convertToUpper
);
94 /** Converts a string to lower case. */
95 std::string
tolower(const std::string
& convertToLower
);
97 /** Calculates the minimum size of each field in the content using the specified fieldcount. */
98 std::vector
<int> calculateMinimumFieldSizes(const std::vector
<Strings
>& content
, int fieldcount
);
100 /** Adjusts the minimum field sizes to take in account the size of the header. */
101 void rebaseMinimumFieldSizes(std::vector
<int>& sizes
, const Strings
& header
);
103 /** Cap the field sizes so that their sum is equal to or less than the specified maximum. */
104 void capFieldSize(std::vector
<int>& sizes
, const int maxlinesize
);
106 /** Cap the fields that are unproporitonally large using the specified attributes. */
107 void capUnfairFields(std::vector
<int>& sizes
, int unfairFields
, int maxFairSize
, int slack
);
109 /** Pack the specified fields into one string using the specified fieldsizes. */
110 std::string
packFields(const Strings
& fields
, const std::vector
<int>& fieldsizes
);
113 /** This is a singleton class, use <code>Get</code> to retreive the instance. */
116 /** This is a singleton class, use <code>Free</code> to free the instance. */
119 friend class Singleton
<String
>;