guessstring.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// \file guessstring.cpp
  3. /// \brief Utility functions for working with strings (std::string and char*)
  4. ///
  5. /// \author Joe Siltberg
  6. /// $Date$
  7. ///
  8. ////////////////////////////////////////////////////////////////////////////////
  9. #include "config.h"
  10. #include "guessstring.h"
  11. #include <cctype>
  12. #include <stdarg.h>
  13. #include <stdio.h>
  14. std::string trim(const std::string& str) {
  15. size_t start_pos = 0;
  16. while (start_pos < str.size() && isspace(str[start_pos])) {
  17. ++start_pos;
  18. }
  19. size_t end_pos = str.size();
  20. while (end_pos > 0 && isspace(str[end_pos-1])) {
  21. --end_pos;
  22. }
  23. if (start_pos < end_pos) {
  24. std::string result(str.begin()+start_pos, str.begin()+end_pos);
  25. return result;
  26. }
  27. else {
  28. return "";
  29. }
  30. }
  31. std::string to_upper(const std::string& str) {
  32. std::string result = str;
  33. for (size_t i = 0; i < result.size(); ++i) {
  34. result[i] = toupper(result[i]);
  35. }
  36. return result;
  37. }
  38. std::string to_lower(const std::string& str) {
  39. std::string result = str;
  40. for (size_t i = 0; i < result.size(); ++i) {
  41. result[i] = tolower(result[i]);
  42. }
  43. return result;
  44. }
  45. std::string format_string(const char* format, ...) {
  46. const size_t buffer_size = 4096;
  47. char buffer[buffer_size];
  48. va_list args;
  49. va_start(args, format);
  50. vsnprintf(buffer, buffer_size, format, args);
  51. return std::string(buffer);
  52. }