Berkeley SfM
Functions
strings Namespace Reference

Functions

void AddExtension (const std::string &extension, std::string *base)
 
std::string CreateTimestampedFilename (const std::string &base_name, const std::string &extension, const char *format_string="%Y%m%d-%H%M%S")
 
bool ExtensionsEquivalent (const std::string &path1, const std::string &path2)
 
bool FilenamesEquivalent (const std::string &path1, const std::string &path2)
 
std::vector< size_t > FindAllOf (const std::string &haystack, const std::string &needle)
 
std::vector< size_t > FindAllOf (const std::string &haystack, char needle)
 
size_t FindFirstOf (const std::string &haystack, const std::string &needle)
 
size_t FindFirstOf (const std::string &haystack, char needle)
 
size_t FindLastOf (const std::string &haystack, const std::string &needle)
 
size_t FindLastOf (const std::string &haystack, char needle)
 
std::string GetDir (const std::string &path)
 
std::string GetExtension (const std::string &string)
 
std::string GetFilename (const std::string &path)
 
std::string GetFilenameNoExtension (const std::string &path)
 
std::string GetPath (const std::string &path)
 
std::string GetRootDir (const std::string &path)
 
bool HasExtension (const std::string &string, const std::string &extension)
 
bool HasPrefix (const std::string &string, const std::string &prefix)
 
bool HasSuffix (const std::string &string, const std::string &suffix)
 
bool IsAbsolutePath (const std::string &path_string)
 
bool IsAlphabetic (const std::string &string)
 
bool IsAscii (const std::string &string)
 
bool IsControl (const std::string &string)
 
bool IsLower (const std::string &string)
 
bool IsNumber (const std::string &string)
 
bool IsUpper (const std::string &string)
 
bool PathsEquivalent (const std::string &path1, const std::string &path2)
 
std::string PrependSlash (const std::string &string)
 
bool RemoveAll (const std::string &needle, std::string *haystack)
 
bool RemoveAll (char needle, std::string *haystack)
 
bool RemoveExtension (std::string *string)
 
bool RemoveFirstOf (const std::string &needle, std::string *haystack)
 
bool RemoveFirstOf (char needle, std::string *haystack)
 
bool RemoveLastOf (const std::string &needle, std::string *haystack)
 
bool RemoveLastOf (char needle, std::string *haystack)
 
bool RemovePrefix (const std::string &prefix, std::string *string)
 
bool RemoveSuffix (const std::string &suffix, std::string *string)
 
std::string Repeat (const std::string &string, int num_repeats)
 
void ReplaceAll (const std::string &replace_this, const std::string &with_this, std::string *string)
 
void ReplaceAll (char replace_this, char with_this, std::string *string)
 
bool ReplaceExtension (const std::string &extension, std::string *base)
 
void Reverse (std::string *string)
 
void ToLower (std::string *string)
 
void ToUpper (std::string *string)
 
void Tokenize (const std::string &string, const std::string &delimiters, std::vector< std::string > *tokens)
 
void Tokenize (const std::string &string, const std::vector< char > &delimiters, std::vector< std::string > *tokens)
 
void Tokenize (const std::string &string, char delimiter, std::vector< std::string > *tokens)
 
void TokenizeFileString (const std::string &string, std::vector< std::string > *tokens)
 
void TrimWhiteSpace (std::string *string)
 

Function Documentation

void strings::AddExtension ( const std::string &  extension,
std::string *  base 
)
inline

Definition at line 29 of file add_extension.h.

30 {
31  if (extension.empty())
32  return;
33 
34  if (extension.front() == '.' || base->back() == '.')
35  *base = Join(*base, extension);
36  else
37  *base = Join(*base, ".", extension);
38 }
std::string Join(const std::vector< std::string > &tokens, const std::string &separator)
Definition: join.h:48
std::string strings::CreateTimestampedFilename ( const std::string &  base_name,
const std::string &  extension,
const char *  format_string = "%Y%m%d-%H%M%S" 
)
inline

Definition at line 41 of file create_timestamped_filename.h.

44  {
45  std::string output_filename, date_time_string;
46 
47  // Get the current date.
48  int size = 12;
49  char *date_time = static_cast<char *>(malloc(size));
50  date_time[0] = '\0';
51  time_t now = time(NULL);
52 
53  // If time exists, format date string.
54  if (now != -1) {
55  // Make sure there is enough room in the buffer to get the full date and
56  // time (specified by format_string). If there is not enough room, retry
57  // until there is.
58  int success = strftime(date_time, size - 1, format_string, gmtime(&now));
59  while (!success) {
60  free(date_time);
61  size += 10;
62  date_time = static_cast<char *>(malloc(size));
63  date_time[0] = '\0';
64  success = strftime(date_time, size - 1, format_string, gmtime(&now));
65  }
66  date_time_string = Join("_", std::string(date_time));
67  }
68  free(date_time);
69  return Join(base_name, date_time_string, ".", extension);
70 }
std::string Join(const std::vector< std::string > &tokens, const std::string &separator)
Definition: join.h:48
bool strings::ExtensionsEquivalent ( const std::string &  path1,
const std::string &  path2 
)
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 strings::FilenamesEquivalent ( const std::string &  path1,
const std::string &  path2 
)
inline

Definition at line 34 of file filenames_equivalent.h.

35  {
36  return GetFilename(path1) == GetFilename(path2);
37 }
std::string GetFilename(const std::string &path)
Definition: get_filename.h:33
std::vector<size_t> strings::FindAllOf ( const std::string &  haystack,
const std::string &  needle 
)
inline

Definition at line 27 of file find_all_of.h.

28  {
29  std::vector<size_t> instances;
30  size_t pos = haystack.find(needle, 0);
31 
32  while (pos != std::string::npos) {
33  instances.push_back(pos);
34  pos = haystack.find(needle, pos + 1);
35  }
36  return instances;
37 }
std::vector<size_t> strings::FindAllOf ( const std::string &  haystack,
char  needle 
)
inline

Definition at line 39 of file find_all_of.h.

39  {
40  const std::string needle_string = { needle };
41  return FindAllOf(haystack, needle_string);
42 }
std::vector< size_t > FindAllOf(const std::string &haystack, char needle)
Definition: find_all_of.h:39
size_t strings::FindFirstOf ( const std::string &  haystack,
const std::string &  needle 
)
inline

Definition at line 27 of file find_first_of.h.

28  {
29  return haystack.find(needle);
30 }
size_t strings::FindFirstOf ( const std::string &  haystack,
char  needle 
)
inline

Definition at line 32 of file find_first_of.h.

32  {
33  const std::string needle_string = { needle };
34  return FindFirstOf(haystack, needle_string);
35 }
size_t FindFirstOf(const std::string &haystack, char needle)
Definition: find_first_of.h:32
size_t strings::FindLastOf ( const std::string &  haystack,
const std::string &  needle 
)
inline

Definition at line 27 of file find_last_of.h.

28  {
29  return haystack.rfind(needle);
30 }
size_t strings::FindLastOf ( const std::string &  haystack,
char  needle 
)
inline

Definition at line 32 of file find_last_of.h.

32  {
33  const std::string needle_string = { needle };
34  return FindLastOf(haystack, needle_string);
35 }
size_t FindLastOf(const std::string &haystack, char needle)
Definition: find_last_of.h:32
std::string strings::GetDir ( const std::string &  path)

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 strings::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 strings::GetFilename ( const std::string &  path)

Definition at line 33 of file get_filename.h.

33  {
34  // If the input path is empty, return an empty filename.
35  if (path.empty())
36  return std::string("");
37 
38  // If the path's last character is '/', there is no filename.
39  if (path.back() == '/')
40  return std::string("");
41 
42  std::vector<std::string> path_tokens;
43  TokenizeFileString(path, &path_tokens);
44  return path_tokens.back();
45 }
bool TokenizeFileString(const std::string &file_string, std::vector< std::string > *tokens)
std::string strings::GetFilenameNoExtension ( const std::string &  path)

Definition at line 34 of file get_filename_no_extension.h.

34  {
35  // If the path's last character is '/', there is no filename.
36  if (path.back() == '/')
37  return std::string("");
38 
39  std::vector<std::string> path_tokens;
40  TokenizeFileString(path, &path_tokens);
41 
42  std::string filename = path_tokens.back();
43  RemoveExtension(&filename);
44  return filename;
45 }
bool RemoveExtension(std::string *string)
bool TokenizeFileString(const std::string &file_string, std::vector< std::string > *tokens)
std::string strings::GetPath ( const std::string &  path)

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 strings::GetRootDir ( const std::string &  path)

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 strings::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 strings::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 strings::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 strings::IsAbsolutePath ( const std::string &  path_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 strings::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 strings::IsAscii ( const std::string &  string)
inline

Definition at line 28 of file is_ascii.h.

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

Definition at line 28 of file is_control.h.

28  {
29  return std::all_of(string.begin(), string.end(), ::iscntrl);
30 }
bool strings::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 strings::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 strings::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 }
bool strings::PathsEquivalent ( const std::string &  path1,
const std::string &  path2 
)
inline

Definition at line 34 of file paths_equivalent.h.

35  {
36  return GetPath(path1) == GetPath(path2);
37 }
std::string GetPath(const std::string &path)
Definition: get_path.h:34
std::string strings::PrependSlash ( const std::string &  string)
inline

Definition at line 27 of file prepend_slash.h.

27  {
28  return std::string("/").append(string);
29 }
bool strings::RemoveAll ( const std::string &  needle,
std::string *  haystack 
)
inline

Definition at line 32 of file remove_all.h.

32  {
33  ReplaceAll(needle, std::string(""), haystack);
34  return haystack->find(needle) != std::string::npos;
35 }
void ReplaceAll(const std::string &replace_this, const std::string &with_this, std::string *string)
Definition: replace_all.h:27
bool strings::RemoveAll ( char  needle,
std::string *  haystack 
)
inline

Definition at line 37 of file remove_all.h.

37  {
38  ReplaceAll(needle, char(0), haystack);
39  return haystack->find(needle) != std::string::npos;
40 }
void ReplaceAll(const std::string &replace_this, const std::string &with_this, std::string *string)
Definition: replace_all.h:27
bool strings::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
bool strings::RemoveFirstOf ( const std::string &  needle,
std::string *  haystack 
)
inline

Definition at line 30 of file remove_first_of.h.

30  {
31  size_t needle_pos = haystack->find(needle);
32  if (needle_pos != std::string::npos) {
33  haystack->erase(needle_pos, needle.size());
34  return true;
35  }
36 
37  return false;
38 }
bool strings::RemoveFirstOf ( char  needle,
std::string *  haystack 
)
inline

Definition at line 40 of file remove_first_of.h.

40  {
41  const std::string needle_string = { needle };
42  return RemoveFirstOf(needle_string, haystack);
43 }
bool RemoveFirstOf(char needle, std::string *haystack)
bool strings::RemoveLastOf ( const std::string &  needle,
std::string *  haystack 
)
inline

Definition at line 30 of file remove_last_of.h.

30  {
31  size_t needle_pos = haystack->rfind(needle);
32  if (needle_pos != std::string::npos) {
33  haystack->erase(needle_pos, needle.size());
34  return true;
35  }
36 
37  return false;
38 }
bool strings::RemoveLastOf ( char  needle,
std::string *  haystack 
)
inline

Definition at line 40 of file remove_last_of.h.

40  {
41  const std::string needle_string = { needle };
42  return RemoveLastOf(needle_string, haystack);
43 }
bool RemoveLastOf(char needle, std::string *haystack)
bool strings::RemovePrefix ( const std::string &  prefix,
std::string *  string 
)
inline

Definition at line 28 of file remove_prefix.h.

28  {
29  if (!HasPrefix(*string, prefix))
30  return false;
31 
32  string->erase(0, prefix.size());
33  return true;
34 }
bool HasPrefix(const std::string &string, const std::string &prefix)
Definition: has_prefix.h:27
bool strings::RemoveSuffix ( const std::string &  suffix,
std::string *  string 
)
inline

Definition at line 28 of file remove_suffix.h.

28  {
29  if (!HasSuffix(*string, suffix))
30  return false;
31 
32  string->erase(string->size() - suffix.size(), suffix.size());
33  return true;
34 }
bool HasSuffix(const std::string &string, const std::string &suffix)
Definition: has_suffix.h:27
std::string strings::Repeat ( const std::string &  string,
int  num_repeats 
)
inline

Definition at line 27 of file repeat.h.

27  {
28  std::string repeated;
29  for (int ii = 0; ii < num_repeats; ++ii)
30  repeated.append(string);
31 
32  return repeated;
33 }
void strings::ReplaceAll ( const std::string &  replace_this,
const std::string &  with_this,
std::string *  string 
)
inline

Definition at line 27 of file replace_all.h.

28  {
29  string->replace(string->begin(), string->end(), replace_this.c_str(),
30  with_this.c_str());
31 }
void strings::ReplaceAll ( char  replace_this,
char  with_this,
std::string *  string 
)
inline

Definition at line 33 of file replace_all.h.

33  {
34  string->replace(string->begin(), string->end(), replace_this, with_this);
35 }
bool strings::ReplaceExtension ( const std::string &  extension,
std::string *  base 
)
inline

Definition at line 30 of file replace_extension.h.

31 {
32  if (!RemoveExtension(base))
33  return false;
34 
35  AddExtension(extension, base);
36  return true;
37 }
bool RemoveExtension(std::string *string)
void AddExtension(const std::string &extension, std::string *base)
Definition: add_extension.h:29
void strings::Reverse ( std::string *  string)
inline

Definition at line 28 of file reverse.h.

28  {
29  std::reverse(string->begin(), string->end());
30 }
void strings::Tokenize ( const std::string &  string,
const std::string &  delimiters,
std::vector< std::string > *  tokens 
)
inline

Definition at line 29 of file tokenize.h.

30  {
31  size_t prev = 0, next = 0;
32  while ((next = string.find_first_of(delimiters, prev)) != std::string::npos) {
33  if (next - prev != 0) {
34  tokens->push_back(string.substr(prev, next - prev));
35  }
36  prev = next + 1;
37  }
38 
39  if (prev < string.size()) {
40  tokens->push_back(string.substr(prev));
41  }
42 }
void strings::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
void strings::Tokenize ( const std::string &  string,
char  delimiter,
std::vector< std::string > *  tokens 
)
inline

Definition at line 56 of file tokenize.h.

57  {
58  std::string delimiter_string = { delimiter };
59  Tokenize(string, delimiter_string, tokens);
60 }
void Tokenize(const std::string &string, char delimiter, std::vector< std::string > *tokens)
Definition: tokenize.h:56
void strings::TokenizeFileString ( const std::string &  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 strings::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 strings::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 }
void strings::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 }