/src/mongo/util/text.h

https://gitlab.com/0072016/0072016-ApplePayDevice- · C Header · 127 lines · 55 code · 25 blank · 47 comment · 1 complexity · bd441c916241c6e188fe4b70cb4e6ba4 MD5 · raw file

  1. // text.h
  2. /*
  3. * Copyright 2010 10gen Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License, version 3,
  7. * as published by the Free Software Foundation.
  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 Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * As a special exception, the copyright holders give permission to link the
  18. * code of portions of this program with the OpenSSL library under certain
  19. * conditions as described in each individual source file and distribute
  20. * linked combinations including the program with the OpenSSL library. You
  21. * must comply with the GNU Affero General Public License in all respects
  22. * for all of the code used other than as permitted herein. If you modify
  23. * file(s) with this exception, you may extend this exception to your
  24. * version of the file(s), but you are not obligated to do so. If you do not
  25. * wish to do so, delete this exception statement from your version. If you
  26. * delete this exception statement from all source files in the program,
  27. * then also delete it in the license file.
  28. */
  29. #pragma once
  30. #include <string>
  31. #include <vector>
  32. #include "mongo/base/disallow_copying.h"
  33. #include "mongo/config.h"
  34. namespace mongo {
  35. class StringSplitter {
  36. public:
  37. /** @param big the std::string to be split
  38. @param splitter the delimiter
  39. */
  40. StringSplitter(const char* big, const char* splitter) : _big(big), _splitter(splitter) {}
  41. /** @return true if more to be taken via next() */
  42. bool more() const {
  43. return _big[0] != 0;
  44. }
  45. /** get next split std::string fragment */
  46. std::string next();
  47. void split(std::vector<std::string>& l);
  48. std::vector<std::string> split();
  49. static std::vector<std::string> split(const std::string& big, const std::string& splitter);
  50. static std::string join(const std::vector<std::string>& l, const std::string& split);
  51. private:
  52. const char* _big;
  53. const char* _splitter;
  54. };
  55. /* This doesn't defend against ALL bad UTF8, but it will guarantee that the
  56. * std::string can be converted to sequence of codepoints. However, it doesn't
  57. * guarantee that the codepoints are valid.
  58. */
  59. bool isValidUTF8(const char* s);
  60. bool isValidUTF8(const std::string& s);
  61. // expect that n contains a base ten number and nothing else after it
  62. // NOTE win version hasn't been tested directly
  63. long long parseLL(const char* n);
  64. #if defined(_WIN32)
  65. std::string toUtf8String(const std::wstring& wide);
  66. std::wstring toWideString(const char* s);
  67. bool writeUtf8ToWindowsConsole(const char* utf8String, unsigned int utf8StringSize);
  68. /* like toWideString but UNICODE macro sensitive */
  69. #if !defined(_UNICODE)
  70. #error temp error
  71. inline std::string toNativeString(const char* s) {
  72. return s;
  73. }
  74. #else
  75. inline std::wstring toNativeString(const char* s) {
  76. return toWideString(s);
  77. }
  78. #endif
  79. class WindowsCommandLine {
  80. MONGO_DISALLOW_COPYING(WindowsCommandLine);
  81. char** _argv;
  82. char** _envp;
  83. public:
  84. WindowsCommandLine(int argc, wchar_t* argvW[], wchar_t* envpW[]);
  85. ~WindowsCommandLine();
  86. char** argv(void) const {
  87. return _argv;
  88. };
  89. char** envp(void) const {
  90. return _envp;
  91. };
  92. };
  93. #endif // #if defined(_WIN32)
  94. /**
  95. * Construct a Windows command line string, UTF-8 encoded, from a vector of
  96. * UTF-8 arguments, "argv".
  97. *
  98. * See "Parsing C++ Command-Line Arguments (C++)"
  99. * http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx
  100. */
  101. std::string constructUtf8WindowsCommandLine(const std::vector<std::string>& argv);
  102. } // namespace mongo