/src/mongo/util/text.h

https://github.com/paralect/mongo · C Header · 122 lines · 54 code · 23 blank · 45 comment · 1 complexity · 21d067900b2d5d644ef019e2791b2d11 MD5 · raw file

  1. /**
  2. * Copyright (C) 2018-present MongoDB, Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the Server Side Public License, version 1,
  6. * as published by MongoDB, Inc.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * Server Side Public License for more details.
  12. *
  13. * You should have received a copy of the Server Side Public License
  14. * along with this program. If not, see
  15. * <http://www.mongodb.com/licensing/server-side-public-license>.
  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 Server Side Public License in all respects for
  22. * all of the code used other than as permitted herein. If you modify file(s)
  23. * with this exception, you may extend this exception to your version of the
  24. * file(s), but you are not obligated to do so. If you do not wish to do so,
  25. * delete this exception statement from your version. If you delete this
  26. * exception statement from all source files in the program, then also delete
  27. * it in the license file.
  28. */
  29. #pragma once
  30. #include <string>
  31. #include <vector>
  32. #include "mongo/base/string_data.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(StringData s);
  60. #if defined(_WIN32)
  61. std::string toUtf8String(const std::wstring& wide);
  62. std::wstring toWideString(const char* s);
  63. bool writeUtf8ToWindowsConsole(const char* utf8String, unsigned int utf8StringSize);
  64. /* like toWideString but UNICODE macro sensitive */
  65. #if !defined(_UNICODE)
  66. #error temp error
  67. inline std::string toNativeString(const char* s) {
  68. return s;
  69. }
  70. #else
  71. inline std::wstring toNativeString(const char* s) {
  72. return toWideString(s);
  73. }
  74. #endif
  75. class WindowsCommandLine {
  76. WindowsCommandLine(const WindowsCommandLine&) = delete;
  77. WindowsCommandLine& operator=(const WindowsCommandLine&) = delete;
  78. char** _argv;
  79. char** _envp;
  80. public:
  81. WindowsCommandLine(int argc, wchar_t* argvW[], wchar_t* envpW[]);
  82. ~WindowsCommandLine();
  83. char** argv(void) const {
  84. return _argv;
  85. };
  86. char** envp(void) const {
  87. return _envp;
  88. };
  89. };
  90. #endif // #if defined(_WIN32)
  91. /**
  92. * Construct a Windows command line string, UTF-8 encoded, from a vector of
  93. * UTF-8 arguments, "argv".
  94. *
  95. * See "Parsing C++ Command-Line Arguments (C++)"
  96. * http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx
  97. */
  98. std::string constructUtf8WindowsCommandLine(const std::vector<std::string>& argv);
  99. } // namespace mongo