StringUtil.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Copyright (c) 2014 ownCloud GmbH. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; version 2.1 of the License
  7. *
  8. * This library is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  11. * details.
  12. */
  13. #ifndef STRINGUTIL_H
  14. #define STRINGUTIL_H
  15. #pragma once
  16. #include <windows.h>
  17. #include <string>
  18. #include <cassert>
  19. class __declspec(dllexport) StringUtil {
  20. public:
  21. static std::string toUtf8(const wchar_t* utf16, int len = -1);
  22. static std::wstring toUtf16(const char* utf8, int len = -1);
  23. template<class T>
  24. static bool begins_with(const T& input, const T& match)
  25. {
  26. return input.size() >= match.size()
  27. && std::equal(match.begin(), match.end(), input.begin());
  28. }
  29. static bool isDescendantOf(const std::wstring& child, const std::wstring& parent) {
  30. return isDescendantOf(child.c_str(), child.size(), parent.c_str(), parent.size());
  31. }
  32. static bool isDescendantOf(PCWSTR child, size_t childLength, const std::wstring& parent) {
  33. return isDescendantOf(child, childLength, parent.c_str(), parent.size());
  34. }
  35. static bool isDescendantOf(PCWSTR child, size_t childLength, PCWSTR parent, size_t parentLength) {
  36. if (!parentLength)
  37. return false;
  38. return (childLength == parentLength || childLength > parentLength && (child[parentLength] == L'\\' || child[parentLength - 1] == L'\\'))
  39. && wcsncmp(child, parent, parentLength) == 0;
  40. }
  41. static bool extractChunks(const std::wstring &source, std::wstring &secondChunk, std::wstring &thirdChunk) {
  42. auto statusBegin = source.find(L':', 0);
  43. assert(statusBegin != std::wstring::npos);
  44. auto statusEnd = source.find(L':', statusBegin + 1);
  45. if (statusEnd == std::wstring::npos) {
  46. // the command do not contains two colon?
  47. return false;
  48. }
  49. // Assume the caller extracted the chunk before the first colon.
  50. secondChunk = source.substr(statusBegin + 1, statusEnd - statusBegin - 1);
  51. thirdChunk = source.substr(statusEnd + 1);
  52. return true;
  53. }
  54. static bool extractChunks(const std::wstring &source, std::wstring &secondChunk, std::wstring &thirdChunk, std::wstring &forthChunk)
  55. {
  56. auto statusBegin = source.find(L':', 0);
  57. assert(statusBegin != std::wstring::npos);
  58. auto statusEnd = source.find(L':', statusBegin + 1);
  59. if (statusEnd == std::wstring::npos) {
  60. // the command do not contains two colon?
  61. return false;
  62. }
  63. auto thirdColon = source.find(L':', statusEnd + 1);
  64. if (statusEnd == std::wstring::npos) {
  65. // the command do not contains three colon?
  66. return false;
  67. }
  68. // Assume the caller extracted the chunk before the first colon.
  69. secondChunk = source.substr(statusBegin + 1, statusEnd - statusBegin - 1);
  70. thirdChunk = source.substr(statusEnd + 1, thirdColon - statusEnd - 1);
  71. forthChunk = source.substr(thirdColon + 1);
  72. return true;
  73. }
  74. };
  75. #endif // STRINGUTIL_H