LogCabin
Core/StringUtil.h
Go to the documentation of this file.
00001 /* Copyright (c) 2011-2012 Stanford University
00002  *
00003  * Permission to use, copy, modify, and distribute this software for any
00004  * purpose with or without fee is hereby granted, provided that the above
00005  * copyright notice and this permission notice appear in all copies.
00006  *
00007  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
00008  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00009  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
00010  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00011  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00012  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00013  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00014  */
00015 
00016 #include <sstream>
00017 #include <string>
00018 #include <vector>
00019 
00020 #ifndef LOGCABIN_CORE_STRINGUTIL_H
00021 #define LOGCABIN_CORE_STRINGUTIL_H
00022 
00023 namespace LogCabin {
00024 namespace Core {
00025 namespace StringUtil {
00026 
00027 /**
00028  * A safe version of sprintf.
00029  */
00030 std::string format(const char* format, ...)
00031     __attribute__((format(printf, 1, 2)));
00032 
00033 /**
00034  * Format an ORed group of flags as a string.
00035  * \param value
00036  *      The ORed options.
00037  * \param flags
00038  *      Maps option identifiers to their string names, such as
00039  *      {{FOO, "FOO"}, {BAR, "BAR"}}.
00040  * \return
00041  *      String such as "FOO|BAR".
00042  */
00043 std::string
00044 flags(int value,
00045       std::initializer_list<std::pair<int, const char*>> flags);
00046 
00047 /**
00048  * Determine whether a null-terminated string is printable.
00049  * \param str
00050  *      A null-terminated string.
00051  * \return
00052  *      True if all the bytes of str before its null terminator are nice to
00053  *      display in a single line of text.
00054  */
00055 bool
00056 isPrintable(const char* str);
00057 
00058 /**
00059  * Determine whether some data is a printable, null-terminated string.
00060  * \param data
00061  *      The first byte.
00062  * \param length
00063  *      The number of bytes of 'data'.
00064  * \return
00065  *      True if the last byte of data is a null terminator and all the bytes of
00066  *      data before that are nice to display in a single line of text.
00067  */
00068 bool
00069 isPrintable(const void* data, size_t length);
00070 
00071 std::string
00072 join(const std::vector<std::string>& components, const std::string& glue);
00073 
00074 /**
00075  * For strings, replace all occurrences of 'needle' in 'haystack' with
00076  * 'replacement'.
00077  *
00078  * If this isn't what you're looking for, the standard algorithm std::replace
00079  * might help you.
00080  */
00081 void
00082 replaceAll(std::string& haystack,
00083            const std::string& needle,
00084            const std::string& replacement);
00085 
00086 /**
00087  * Split a string into multiple components by a character.
00088  * \param subject
00089  *      The string to split.
00090  * \param delimiter
00091  *      The character to split the string by.
00092  * \return
00093  *      The components of 'subject', not including 'delimiter'.
00094  *      - If two delimiters occur in a row in 'subject', a corresponding empty
00095  *        string will appear in the returned vector.
00096  *      - If a delimiter occurs at the start of 'subject', a corresponding
00097  *        empty string will appear at the start of the returned vector.
00098  *      - If a delimiter occurs at the end of 'subject', no corresponding empty
00099  *        string will appear at the end of the returned vector.
00100  */
00101 std::vector<std::string>
00102 split(const std::string& subject, char delimiter);
00103 
00104 /// Return true if haystack begins with needle.
00105 bool startsWith(const std::string& haystack, const std::string& needle);
00106 
00107 /// Return true if haystack ends with needle.
00108 bool endsWith(const std::string& haystack, const std::string& needle);
00109 
00110 /**
00111  * Return a string returned from the given object's stream operator.
00112  * This is useful when you're dealing with strings, but the object you want to
00113  * print only has a stream operator.
00114  */
00115 template<typename T>
00116 std::string
00117 toString(const T& t)
00118 {
00119     std::stringstream ss;
00120     ss << t;
00121     return ss.str();
00122 }
00123 
00124 /**
00125  * Return a copy of the given string except with no leading or trailing
00126  * whitespace.
00127  */
00128 std::string trim(const std::string& s);
00129 
00130 } // namespace LogCabin::Core::StringUtil
00131 } // namespace LogCabin::Core
00132 } // namespace LogCabin
00133 
00134 #endif /* LOGCABIN_CORE_STRINGUTIL_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines