Berkeley SfM
string_utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 - Erik Nelson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 
20 #ifndef __STRING_STRING_UTILS_H__
21 #define __STRING_STRING_UTILS_H__
22 
23 #include <string>
24 #include <vector>
25 
26 namespace string {
27 
28 bool IsUpper(const std::string &string);
29 
30 bool IsLower(const std::string &string);
31 
32 bool IsNumber(const std::string &string);
33 
34 bool IsAlphabetic(const std::string &string);
35 
36 bool HasPrefix(const std::string &string, const std::string &prefix);
37 
38 bool HasSuffix(const std::string &string, const std::string &suffix);
39 
40 bool HasExtension(const std::string &string, const std::string &extension);
41 
42 void ToUpper(std::string *string);
43 
44 void ToLower(std::string *string);
45 
46 bool ReplaceAll(std::string *string, const std::string &replace_this,
47  const std::string &with_this);
48 
49 bool ReplaceAllChar(std::string *string, char replace_this, char with_this);
50 
51 bool RemoveNumbers(std::string *string);
52 
53 // base = hello_world, extension = .png
54 // if include_date is true --> hello_world_2015_06_04_9-45-16.png
55 // otherwise --> hello_world_9-45-16.png
56 std::string CreateTimestampedFilename(const std::string &base,
57  const std::string &extension,
58  const bool include_date = true);
59 
60 // users/erik/yodawg.jpg --> erik
61 std::string GetDir(const std::string &string);
62 
63 // users/erik/yodawg.jpg --> users/erik
64 std::string GetPath(const std::string &string);
65 
66 // users/erik/yodawg.jpg --> users
67 std::string GetRootDir(const std::string &string);
68 
69 // users/erik/yodawg.jpg --> yodawg.jpg
70 std::string GetFileName(const std::string &string);
71 
72 // users/erik/yodawg.jpg --> yodawg
73 std::string GetFileNameNoExtension(const std::string &string);
74 
75 // /users/erik/yodawg.jpg --> /users/erik/yodawg
76 // yodawg.jpg --> yodawg
77 bool RemoveExtension(std::string *string);
78 
79 bool RemovePrefix(std::string *string);
80 
81 bool RemoveSuffix(std::string *string);
82 
83 // /users/erik/yodawg.jpg --> jpg
84 std::string GetExtension(const std::string &string);
85 
86 void ReplaceExtension(std::string *string, const std::string &new_extension);
87 
88 std::string JoinFilePath(const std::vector<std::string> &strings);
89 
90 bool IsAbsolutePath(const std::string &string);
91 
92 bool Tokenize(const std::string &string, const std::vector<char> &delimiters,
93  std::vector<std::string> *tokens);
94 
95 bool TokenizeFileString(const std::string &file_string,
96  std::vector<std::string> *tokens);
97 
98 void TrimWhiteSpace(std::string *string);
99 
100 std::string StringPrintf(const std::string &string);
101 
102 std::string StringScanf(const std::string &string);
103 
104 size_t FindFirstOf(const std::string &string);
105 
106 size_t FindLastOf(const std::string &string);
107 
108 std::vector<size_t> FindAllOf(const std::string &string);
109 
110 void RemoveFirstOf(std::string *string, const std::string &remove_this);
111 
112 void RemoveLastOf(std::string *string, const std::string &remove_this);
113 
114 void RemoveAll(std::string *string, const std::string &remove_this);
115 
116 std::string Substring(const std::string &string);
117 
118 // string = hello_world, after_this = hello_
119 // --> world
120 std::string After(const std::string &string, const std::string &after_this);
121 
122 std::string Before(const std::string &string, const std::string &before_this);
123 
124 void Append(std::string *base, const std::string &append);
125 
126 void Append(std::string *first, const std::vector<std::string> &second);
127 
128 bool TrimFromBackToLength(std::string *string, size_t length);
129 
130 bool TrimFromFrontToLength(std::string *string, size_t length);
131 
132 // bool TrimFromPosToLength(std::string *string, size_t start, size_t length);
133 
134 bool TrimFromTo(std::string *string, size_t from, size_t to);
135 
136 bool FileNamesEquivalent(const std::string &one, const std::string &two);
137 
138 bool ExtensionsEquivalent(const std::string &one, const std::string &two);
139 
140 bool FilePathsEquivalent(const std::string &one, const std::string &two);
141 
142 void Reverse(std::string *string);
143 } //\namespace string
144 
145 #endif
void ReplaceExtension(std::string *string, const std::string &new_extension)
bool TrimFromBackToLength(std::string *string, size_t length)
void ToLower(std::string *string)
Definition: to_lower.h:28
void Append(std::string *base, const std::string &append)
std::string GetRootDir(const std::string &string)
Definition: get_root_dir.h:33
bool ExtensionsEquivalent(const std::string &one, const std::string &two)
std::string CreateTimestampedFilename(const std::string &base, const std::string &extension, const bool include_date=true)
size_t FindLastOf(const std::string &string)
bool RemovePrefix(std::string *string)
bool TrimFromFrontToLength(std::string *string, size_t length)
bool IsUpper(const std::string &string)
Definition: is_upper.h:28
bool IsLower(const std::string &string)
Definition: is_lower.h:28
std::string Before(const std::string &string, const std::string &before_this)
std::string GetPath(const std::string &string)
Definition: get_path.h:34
bool RemoveSuffix(std::string *string)
std::vector< size_t > FindAllOf(const std::string &string)
std::string GetFileNameNoExtension(const std::string &string)
bool RemoveNumbers(std::string *string)
bool TokenizeFileString(const std::string &file_string, std::vector< std::string > *tokens)
void ToUpper(std::string *string)
Definition: to_upper.h:28
void RemoveAll(std::string *string, const std::string &remove_this)
bool HasPrefix(const std::string &string, const std::string &prefix)
Definition: has_prefix.h:27
std::string StringPrintf(const std::string &string)
bool RemoveExtension(std::string *string)
std::string GetExtension(const std::string &string)
Definition: get_extension.h:27
std::string JoinFilePath(const std::vector< std::string > &strings)
std::string GetDir(const std::string &string)
Definition: get_dir.h:32
bool IsNumber(const std::string &string)
Definition: is_number.h:28
void RemoveLastOf(std::string *string, const std::string &remove_this)
std::string StringScanf(const std::string &string)
bool FileNamesEquivalent(const std::string &one, const std::string &two)
std::string After(const std::string &string, const std::string &after_this)
bool TrimFromTo(std::string *string, size_t from, size_t to)
void TrimWhiteSpace(std::string *string)
std::string GetFileName(const std::string &string)
size_t FindFirstOf(const std::string &string)
bool ReplaceAll(std::string *string, const std::string &replace_this, const std::string &with_this)
bool HasSuffix(const std::string &string, const std::string &suffix)
Definition: has_suffix.h:27
std::string Substring(const std::string &string)
bool FilePathsEquivalent(const std::string &one, const std::string &two)
bool ReplaceAllChar(std::string *string, char replace_this, char with_this)
bool HasExtension(const std::string &string, const std::string &extension)
Definition: has_extension.h:27
bool IsAlphabetic(const std::string &string)
Definition: is_alphabetic.h:28
void RemoveFirstOf(std::string *string, const std::string &remove_this)
bool IsAbsolutePath(const std::string &string)
bool Tokenize(const std::string &string, const std::vector< char > &delimiters, std::vector< std::string > *tokens)
Definition: tokenize.h:45
void Reverse(std::string *string)
Definition: reverse.h:28