Merge pull request #4676 from jmarshallnz/dont_set_scraper_on_tvshow_on_nfo
[vuplus_xbmc] / xbmc / utils / StringUtils.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://xbmc.org
5  *
6  *  This Program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This Program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21 //-----------------------------------------------------------------------
22 //
23 //  File:      StringUtils.h
24 //
25 //  Purpose:   ATL split string utility
26 //  Author:    Paul J. Weiss
27 //
28 //  Modified to support J O'Leary's CStdString class by kraqh3d
29 //
30 //------------------------------------------------------------------------
31
32 #include <vector>
33 #include <stdint.h>
34 #include <string>
35
36 #include "XBDateTime.h"
37 #include "utils/StdString.h"
38
39 class StringUtils
40 {
41 public:
42   /*! \brief Get a formatted string similar to sprintf
43
44   Beware that this does not support directly passing in
45   std::string objects. You need to call c_str() to pass
46   the const char* buffer representing the value of the
47   std::string object.
48
49   \param fmt Format of the resulting string
50   \param ... variable number of value type arguments
51   \return Formatted string
52   */
53   static std::string Format(const char *fmt, ...);
54   static std::string FormatV(const char *fmt, va_list args);
55   static std::wstring Format(const wchar_t *fmt, ...);
56   static std::wstring FormatV(const wchar_t *fmt, va_list args);
57   static void ToUpper(std::string &str);
58   static void ToUpper(std::wstring &str);
59   static void ToLower(std::string &str);
60   static void ToLower(std::wstring &str);
61   static bool EqualsNoCase(const std::string &str1, const std::string &str2);
62   static bool EqualsNoCase(const std::string &str1, const char *s2);
63   static bool EqualsNoCase(const char *s1, const char *s2);
64   static int  CompareNoCase(const std::string &str1, const std::string &str2);
65   static int  CompareNoCase(const char *s1, const char *s2);
66   static std::string Left(const std::string &str, size_t count);
67   static std::string Mid(const std::string &str, size_t first, size_t count = std::string::npos);
68   static std::string Right(const std::string &str, size_t count);
69   static std::string& Trim(std::string &str);
70   static std::string& Trim(std::string &str, const char* const chars);
71   static std::string& TrimLeft(std::string &str);
72   static std::string& TrimLeft(std::string &str, const char* const chars);
73   static std::string& TrimRight(std::string &str);
74   static std::string& TrimRight(std::string &str, const char* const chars);
75   static std::string& RemoveDuplicatedSpacesAndTabs(std::string& str);
76   static int Replace(std::string &str, char oldChar, char newChar);
77   static int Replace(std::string &str, const std::string &oldStr, const std::string &newStr);
78   static int Replace(std::wstring &str, const std::wstring &oldStr, const std::wstring &newStr);
79   static bool StartsWith(const std::string &str1, const std::string &str2);
80   static bool StartsWith(const std::string &str1, const char *s2);
81   static bool StartsWith(const char *s1, const char *s2);
82   static bool StartsWithNoCase(const std::string &str1, const std::string &str2);
83   static bool StartsWithNoCase(const std::string &str1, const char *s2);
84   static bool StartsWithNoCase(const char *s1, const char *s2);
85   static bool EndsWith(const std::string &str1, const std::string &str2);
86   static bool EndsWith(const std::string &str1, const char *s2);
87   static bool EndsWithNoCase(const std::string &str1, const std::string &str2);
88   static bool EndsWithNoCase(const std::string &str1, const char *s2);
89
90   static void JoinString(const CStdStringArray &strings, const CStdString& delimiter, CStdString& result);
91   static CStdString JoinString(const CStdStringArray &strings, const CStdString& delimiter);
92   static CStdString Join(const std::vector<std::string> &strings, const CStdString& delimiter);
93   static int SplitString(const CStdString& input, const CStdString& delimiter, CStdStringArray &results, unsigned int iMaxStrings = 0);
94   static CStdStringArray SplitString(const CStdString& input, const CStdString& delimiter, unsigned int iMaxStrings = 0);
95   static std::vector<std::string> Split(const std::string& input, const std::string& delimiter, unsigned int iMaxStrings = 0);
96   static int FindNumber(const CStdString& strInput, const CStdString &strFind);
97   static int64_t AlphaNumericCompare(const wchar_t *left, const wchar_t *right);
98   static long TimeStringToSeconds(const CStdString &timeString);
99   static void RemoveCRLF(CStdString& strLine);
100
101   /*! \brief utf8 version of strlen - skips any non-starting bytes in the count, thus returning the number of utf8 characters
102    \param s c-string to find the length of.
103    \return the number of utf8 characters in the string.
104    */
105   static size_t utf8_strlen(const char *s);
106
107   /*! \brief convert a time in seconds to a string based on the given time format
108    \param seconds time in seconds
109    \param format the format we want the time in.
110    \return the formatted time
111    \sa TIME_FORMAT
112    */
113   static CStdString SecondsToTimeString(long seconds, TIME_FORMAT format = TIME_FORMAT_GUESS);
114
115   /*! \brief check whether a string is a natural number.
116    Matches [ \t]*[0-9]+[ \t]*
117    \param str the string to check
118    \return true if the string is a natural number, false otherwise.
119    */
120   static bool IsNaturalNumber(const CStdString& str);
121
122   /*! \brief check whether a string is an integer.
123    Matches [ \t]*[\-]*[0-9]+[ \t]*
124    \param str the string to check
125    \return true if the string is an integer, false otherwise.
126    */
127   static bool IsInteger(const CStdString& str);
128
129   /* The next several isasciiXX and asciiXXvalue functions are locale independent (US-ASCII only),
130    * as opposed to standard ::isXX (::isalpha, ::isdigit...) which are locale dependent.
131    * Next functions get parameter as char and don't need double cast ((int)(unsigned char) is required for standard functions). */
132   inline static bool isasciidigit(char chr) // locale independent 
133   {
134     return chr >= '0' && chr <= '9'; 
135   }
136   inline static bool isasciixdigit(char chr) // locale independent 
137   {
138     return (chr >= '0' && chr <= '9') || (chr >= 'a' && chr <= 'f') || (chr >= 'A' && chr <= 'F'); 
139   }
140   static int asciidigitvalue(char chr); // locale independent 
141   static int asciixdigitvalue(char chr); // locale independent 
142   inline static bool isasciiuppercaseletter(char chr) // locale independent
143   {
144     return (chr >= 'A' && chr <= 'Z'); 
145   }
146   inline static bool isasciilowercaseletter(char chr) // locale independent
147   {
148     return (chr >= 'a' && chr <= 'z'); 
149   }
150   inline static bool isasciialphanum(char chr) // locale independent
151   {
152     return isasciiuppercaseletter(chr) || isasciilowercaseletter(chr) || isasciidigit(chr); 
153   }
154   static CStdString SizeToString(int64_t size);
155   static const CStdString EmptyString;
156   static const std::string Empty;
157   static size_t FindWords(const char *str, const char *wordLowerCase);
158   static int FindEndBracket(const CStdString &str, char opener, char closer, int startPos = 0);
159   static int DateStringToYYYYMMDD(const CStdString &dateString);
160   static void WordToDigits(CStdString &word);
161   static CStdString CreateUUID();
162   static bool ValidateUUID(const CStdString &uuid); // NB only validates syntax
163   static double CompareFuzzy(const CStdString &left, const CStdString &right);
164   static int FindBestMatch(const CStdString &str, const CStdStringArray &strings, double &matchscore);
165   static bool ContainsKeyword(const CStdString &str, const CStdStringArray &keywords);
166
167   /*! \brief Escapes the given string to be able to be used as a parameter.
168
169    Escapes backslashes and double-quotes with an additional backslash and
170    adds double-quotes around the whole string.
171
172    \param param String to escape/paramify
173    \return Escaped/Paramified string
174    */
175   static std::string Paramify(const std::string &param);
176   static void Tokenize(const std::string& input, std::vector<std::string>& tokens, const std::string& delimiters);
177 private:
178   static CStdString m_lastUUID;
179 };