util_string.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __UTIL_STRING_H__
  17. #define __UTIL_STRING_H__
  18. #include <string.h>
  19. #include <string>
  20. #include <sstream>
  21. #include "util/util_vector.h"
  22. CCL_NAMESPACE_BEGIN
  23. using std::istringstream;
  24. using std::ostringstream;
  25. using std::string;
  26. using std::stringstream;
  27. using std::to_string;
  28. #ifdef __GNUC__
  29. # define PRINTF_ATTRIBUTE __attribute__((format(printf, 1, 2)))
  30. #else
  31. # define PRINTF_ATTRIBUTE
  32. #endif
  33. string string_printf(const char *format, ...) PRINTF_ATTRIBUTE;
  34. bool string_iequals(const string &a, const string &b);
  35. void string_split(vector<string> &tokens,
  36. const string &str,
  37. const string &separators = "\t ",
  38. bool skip_empty_tokens = true);
  39. void string_replace(string &haystack, const string &needle, const string &other);
  40. bool string_startswith(const string &s, const char *start);
  41. bool string_endswith(const string &s, const char *end);
  42. string string_strip(const string &s);
  43. string string_remove_trademark(const string &s);
  44. string string_from_bool(const bool var);
  45. string to_string(const char *str);
  46. /* Wide char strings are only used on Windows to deal with non-ascii
  47. * characters in file names and such. No reason to use such strings
  48. * for something else at this moment.
  49. *
  50. * Please note that strings are expected to be in UTF-8 codepage, and
  51. * if ANSI is needed then explicit conversion required.
  52. */
  53. #ifdef _WIN32
  54. using std::wstring;
  55. wstring string_to_wstring(const string &path);
  56. string string_from_wstring(const wstring &path);
  57. string string_to_ansi(const string &str);
  58. #endif
  59. /* Make a string from a size in bytes in human readable form */
  60. string string_human_readable_size(size_t size);
  61. /* Make a string from a unitless quantity in human readable form */
  62. string string_human_readable_number(size_t num);
  63. CCL_NAMESPACE_END
  64. #endif /* __UTIL_STRING_H__ */