Sorted Resource.
[UnsignedByte.git] / src / Resource / StringUtilities.h
blob6574f8a762de9175c6c43c0a460cba6218b5aad8
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 #pragma once
22 /**
23 * @file StringUtilities.h
24 * This file contains the String class.
26 * @see String
27 */
29 #include "Global.h"
30 #include "Types.h"
32 /**
33 * This class provides a set of utility functions that operate on strings.
34 */
35 class String : public Singleton<String>
37 public:
38 /**
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.
44 */
45 Strings lines(const std::string& input, const char* separator = "\n");
47 /**
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.
53 */
54 std::string unlines(const Strings& input, const char* filler, int newlineat = 1);
56 /**
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.
61 */
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);
67 /**
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.
73 */
74 std::string box(const Strings& content, const std::string& header = Global::Get()->EmptyString);
76 /**
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.
82 */
83 Strings createTable(const std::vector<Strings>& content, const Strings& header);
85 /** Returns the 'count word' representation of an integer ('1st', '2nd', '3rd', etc). */
86 std::string countWord(value_type value);
88 /** Returns a string representation of an integer. */
89 std::string fromInt(value_type value);
91 /** Returns a string representation of an integer. */
92 std::string fromLong(signed long value);
94 /** Converts a string to upper case. */
95 std::string toupper(const std::string& convertToUpper);
97 /** Converts a string to lower case. */
98 std::string tolower(const std::string& convertToLower);
100 /** Calculates the minimum size of each field in the content using the specified fieldcount. */
101 std::vector<int> calculateMinimumFieldSizes(const std::vector<Strings>& content, int fieldcount);
103 /** Adjusts the minimum field sizes to take in account the size of the header. */
104 void rebaseMinimumFieldSizes(std::vector<int>& sizes, const Strings& header);
106 /** Cap the field sizes so that their sum is equal to or less than the specified maximum. */
107 void capFieldSize(std::vector<int>& sizes, const int maxlinesize);
109 /** Cap the fields that are unproporitonally large using the specified attributes. */
110 void capUnfairFields(std::vector<int>& sizes, int unfairFields, int maxFairSize, int slack);
112 /** Pack the specified fields into one string using the specified fieldsizes. */
113 std::string packFields(const Strings& fields, const std::vector<int>& fieldsizes);
115 private:
116 /** This is a singleton class, use <code>Get</code> to retreive the instance. */
117 String(void) {};
119 /** This is a singleton class, use <code>Free</code> to free the instance. */
120 ~String(void) {};
122 friend class Singleton<String>;