Berkeley SfM
Functions
string Namespace Reference

Functions

bool IsUpper (const std::string &string)
 
bool IsLower (const std::string &string)
 
bool IsNumber (const std::string &string)
 
bool IsAlphabetic (const std::string &string)
 
bool HasPrefix (const std::string &string, const std::string &prefix)
 
bool HasSuffix (const std::string &string, const std::string &suffix)
 
bool HasExtension (const std::string &string, const std::string &extension)
 
void ToUpper (std::string *string)
 
void ToLower (std::string *string)
 
bool ReplaceAll (std::string *string, const std::string &replace_this, const std::string &with_this)
 
bool ReplaceAllChar (std::string *string, char replace_this, char with_this)
 
bool RemoveNumbers (std::string *string)
 
std::string CreateTimestampedFilename (const std::string &base, const std::string &extension, const bool include_date=true)
 
std::string GetDir (const std::string &string)
 
std::string GetPath (const std::string &string)
 
std::string GetRootDir (const std::string &string)
 
std::string GetFileName (const std::string &string)
 
std::string GetFileNameNoExtension (const std::string &string)
 
bool RemoveExtension (std::string *string)
 
bool RemovePrefix (std::string *string)
 
bool RemoveSuffix (std::string *string)
 
std::string GetExtension (const std::string &string)
 
void ReplaceExtension (std::string *string, const std::string &new_extension)
 
std::string JoinFilePath (const std::vector< std::string > &strings)
 
bool IsAbsolutePath (const std::string &string)
 
bool Tokenize (const std::string &string, const std::vector< char > &delimiters, std::vector< std::string > *tokens)
 
bool TokenizeFileString (const std::string &file_string, std::vector< std::string > *tokens)
 
void TrimWhiteSpace (std::string *string)
 
std::string StringPrintf (const std::string &string)
 
std::string StringScanf (const std::string &string)
 
size_t FindFirstOf (const std::string &string)
 
size_t FindLastOf (const std::string &string)
 
std::vector< size_t > FindAllOf (const std::string &string)
 
void RemoveFirstOf (std::string *string, const std::string &remove_this)
 
void RemoveLastOf (std::string *string, const std::string &remove_this)
 
void RemoveAll (std::string *string, const std::string &remove_this)
 
std::string Substring (const std::string &string)
 
std::string After (const std::string &string, const std::string &after_this)
 
std::string Before (const std::string &string, const std::string &before_this)
 
void Append (std::string *base, const std::string &append)
 
void Append (std::string *first, const std::vector< std::string > &second)
 
bool TrimFromBackToLength (std::string *string, size_t length)
 
bool TrimFromFrontToLength (std::string *string, size_t length)
 
bool TrimFromTo (std::string *string, size_t from, size_t to)
 
bool FileNamesEquivalent (const std::string &one, const std::string &two)
 
bool ExtensionsEquivalent (const std::string &one, const std::string &two)
 
bool FilePathsEquivalent (const std::string &one, const std::string &two)
 
void Reverse (std::string *string)
 

Function Documentation

std::string string::After ( const std::string &  string,
const std::string &  after_this 
)
void string::Append ( std::string *  base,
const std::string &  append 
)
void string::Append ( std::string *  first,
const std::vector< std::string > &  second 
)
std::string string::Before ( const std::string &  string,
const std::string &  before_this 
)
std::string string::CreateTimestampedFilename ( const std::string &  base,
const std::string &  extension,
const bool  include_date = true 
)
bool string::ExtensionsEquivalent ( const std::string &  one,
const std::string &  two 
)
inline

Definition at line 34 of file extensions_equivalent.h.

35  {
36  return GetExtension(path1) == GetExtension(path2);
37 }
std::string GetExtension(const std::string &string)
Definition: get_extension.h:27
bool string::FileNamesEquivalent ( const std::string &  one,
const std::string &  two 
)
bool string::FilePathsEquivalent ( const std::string &  one,
const std::string &  two 
)
std::vector<size_t> string::FindAllOf ( const std::string &  string)
size_t string::FindFirstOf ( const std::string &  string)
size_t string::FindLastOf ( const std::string &  string)
std::string string::GetDir ( const std::string &  string)

Definition at line 32 of file get_dir.h.

32  {
33  std::vector<std::string> path_tokens;
34  TokenizeFileString(path, &path_tokens);
35 
36  // If the input path ends in a '/', it already does not have a filename. The
37  // last element in the tokens vector is the local directory.
38  // e.g. this/is/a/path/ --> return "path"
39  if (path.back() == '/')
40  return path_tokens.back();
41 
42  // Otherwise, there must be at least 2 tokens to have a local directory.
43  // e.g. if the path is "/filename", there is no way to tell if it is a file or
44  // directory. Assume it is a file.
45  if (path_tokens.size() < 2)
46  return std::string("");
47 
48  // If there are at least 2 tokens, return the second-to-last.
49  // e.g. local_directory/filename.ext --> local_directory
50  return path_tokens[path_tokens.size()-2];
51 }
bool TokenizeFileString(const std::string &file_string, std::vector< std::string > *tokens)
std::string string::GetExtension ( const std::string &  string)
inline

Definition at line 27 of file get_extension.h.

27  {
28 
29  // Extension begins after the last '.', if one exists.
30  const size_t last_period = string.find_last_of('.');
31  if (last_period == string.npos)
32  return std::string("");
33 
34  // Return an empty extension if the last charaacter in the string is a period.
35  if (string.back() == '.')
36  return std::string("");
37 
38  return string.substr(last_period + 1, string.size() - last_period - 1);
39 }
std::string string::GetFileName ( const std::string &  string)
std::string string::GetFileNameNoExtension ( const std::string &  string)
std::string string::GetPath ( const std::string &  string)

Definition at line 34 of file get_path.h.

34  {
35  // If the input path is empty, return an empty string.
36  if (path.empty())
37  return std::string("");
38 
39  // If the input path ends in a '/', it already does not have a filename.
40  if (path.back() == '/')
41  return path;
42 
43  std::vector<std::string> path_tokens;
44  TokenizeFileString(path, &path_tokens);
45 
46  // If there are no tokens and the path begins with '/', return '/'. Otherwise
47  // the input name is a file and therefore has no path.
48  if (path_tokens.size() == 0)
49  return path[0] == '/' ? std::string("/") : std::string("");
50 
51  // Remove the last token from the tokenized path.
52  path_tokens.erase(path_tokens.end());
53 
54  // Join the remaining tokens, add a prepending slash if one existed in the
55  // input path, and return the result.
56  std::string output_path = JoinFilepath(path_tokens);
57  if (path[0] == '/')
58  return PrependSlash(output_path);
59 
60  return output_path;
61 }
std::string PrependSlash(const std::string &string)
Definition: prepend_slash.h:27
bool TokenizeFileString(const std::string &file_string, std::vector< std::string > *tokens)
std::string JoinFilepath(const std::vector< std::string > &path_tokens)
Definition: join_filepath.h:49
std::string string::GetRootDir ( const std::string &  string)

Definition at line 33 of file get_root_dir.h.

33  {
34  std::vector<std::string> path_tokens;
35  TokenizeFileString(path, &path_tokens);
36 
37  // If there are no tokens or only one token in the file path, return an empty string.
38  if (path_tokens.size() <= 1)
39  return std::string("");
40 
41  return path_tokens.front();
42 }
bool TokenizeFileString(const std::string &file_string, std::vector< std::string > *tokens)
bool string::HasExtension ( const std::string &  string,
const std::string &  extension 
)
inline

Definition at line 27 of file has_extension.h.

28  {
29  // - 1 because extension does not include the '.'
30  if (extension.size() > string.size() - 1)
31  return false;
32 
33  // Extension begins after the last '.', if one exists.
34  const size_t last_period = string.find_last_of('.');
35  if (last_period == string.npos)
36  return false;
37 
38  // If the extension has non-zero size, '.' cannot be the last character in the
39  // string.
40  if (string.back() == '.' && extension.size() > 0)
41  return false;
42 
43  // If the user prepends their extension query with '.', include the string's
44  // last period in the comparison.
45  if (extension[0] == '.')
46  return string.compare(last_period, extension.size(), extension) == 0;
47 
48  return string.compare(last_period + 1, extension.size(), extension) == 0;
49 }
bool string::HasPrefix ( const std::string &  string,
const std::string &  prefix 
)
inline

Definition at line 27 of file has_prefix.h.

27  {
28  return string.size() >= prefix.size() &&
29  string.compare(0, prefix.size(), prefix) == 0;
30 }
bool string::HasSuffix ( const std::string &  string,
const std::string &  suffix 
)
inline

Definition at line 27 of file has_suffix.h.

27  {
28  const size_t suffix_begin = string.size() - suffix.size();
29  return string.size() >= suffix.size() &&
30  string.compare(suffix_begin, suffix.size(), suffix) == 0;
31 }
bool string::IsAbsolutePath ( const std::string &  string)
inline

Definition at line 27 of file is_absolute_path.h.

27  {
28  if (path_string.empty())
29  return false;
30 
31  return path_string[0] == '/';
32 }
bool string::IsAlphabetic ( const std::string &  string)
inline

Definition at line 28 of file is_alphabetic.h.

28  {
29  return std::all_of(string.begin(), string.end(), ::isalpha);
30 }
bool string::IsLower ( const std::string &  string)
inline

Definition at line 28 of file is_lower.h.

28  {
29  return std::all_of(string.begin(), string.end(), ::islower);
30 }
bool string::IsNumber ( const std::string &  string)
inline

Definition at line 28 of file is_number.h.

28  {
29  return std::all_of(string.begin(), string.end(), ::isdigit);
30 }
bool string::IsUpper ( const std::string &  string)
inline

Definition at line 28 of file is_upper.h.

28  {
29  return std::all_of(string.begin(), string.end(), ::isupper);
30 }
std::string string::JoinFilePath ( const std::vector< std::string > &  strings)
void string::RemoveAll ( std::string *  string,
const std::string &  remove_this 
)
bool string::RemoveExtension ( std::string *  string)
inline

Definition at line 30 of file remove_extension.h.

30  {
31  const std::string extension = GetExtension(*string);
32  if (extension.empty() && string->back() != '.')
33  return false;
34 
35  std::string period(".");
36  return RemoveSuffix(period.append(extension), string);
37 }
std::string GetExtension(const std::string &string)
Definition: get_extension.h:27
bool RemoveSuffix(const std::string &suffix, std::string *string)
Definition: remove_suffix.h:28
void string::RemoveFirstOf ( std::string *  string,
const std::string &  remove_this 
)
void string::RemoveLastOf ( std::string *  string,
const std::string &  remove_this 
)
bool string::RemoveNumbers ( std::string *  string)
bool string::RemovePrefix ( std::string *  string)
bool string::RemoveSuffix ( std::string *  string)
bool string::ReplaceAll ( std::string *  string,
const std::string &  replace_this,
const std::string &  with_this 
)
bool string::ReplaceAllChar ( std::string *  string,
char  replace_this,
char  with_this 
)
void string::ReplaceExtension ( std::string *  string,
const std::string &  new_extension 
)
void string::Reverse ( std::string *  string)
inline

Definition at line 28 of file reverse.h.

28  {
29  std::reverse(string->begin(), string->end());
30 }
std::string string::StringPrintf ( const std::string &  string)
std::string string::StringScanf ( const std::string &  string)
std::string string::Substring ( const std::string &  string)
bool string::Tokenize ( const std::string &  string,
const std::vector< char > &  delimiters,
std::vector< std::string > *  tokens 
)
inline

Definition at line 45 of file tokenize.h.

47  {
48  std::string delimiters_string;
49  for (const auto &element : delimiters)
50  delimiters_string.push_back(element);
51 
52  Tokenize(string, delimiters_string, tokens);
53 }
void Tokenize(const std::string &string, char delimiter, std::vector< std::string > *tokens)
Definition: tokenize.h:56
bool string::TokenizeFileString ( const std::string &  file_string,
std::vector< std::string > *  tokens 
)
inline

Definition at line 33 of file tokenize_file_string.h.

34  {
35  Tokenize(string, '/', tokens);
36 }
bool Tokenize(const std::string &string, const std::vector< char > &delimiters, std::vector< std::string > *tokens)
Definition: tokenize.h:45
void string::ToLower ( std::string *  string)
inline

Definition at line 28 of file to_lower.h.

28  {
29  std::transform(string->begin(), string->end(), string->begin(), ::tolower);
30 }
void string::ToUpper ( std::string *  string)
inline

Definition at line 28 of file to_upper.h.

28  {
29  std::transform(string->begin(), string->end(), string->begin(), ::toupper);
30 }
bool string::TrimFromBackToLength ( std::string *  string,
size_t  length 
)
bool string::TrimFromFrontToLength ( std::string *  string,
size_t  length 
)
bool string::TrimFromTo ( std::string *  string,
size_t  from,
size_t  to 
)
void string::TrimWhiteSpace ( std::string *  string)
inline

Definition at line 27 of file trim_white_space.h.

27  {
28  // Trim white space from the back.
29  size_t back_whitespace = string->size() - 1;
30  for (; back_whitespace > 0; --back_whitespace) {
31  if (string->at(back_whitespace) != ' ')
32  break;
33  }
34  string->erase(back_whitespace + 1, string->size());
35 
36  // Trim white space from the front.
37  size_t front_whitespace = 0;
38  for (; front_whitespace < string->size(); ++front_whitespace) {
39  if (string->at(front_whitespace) != ' ')
40  break;
41  }
42  string->erase(0, front_whitespace);
43 }