File: StringUtil.h

package info (click to toggle)
nextcloud-desktop 4.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,740 kB
  • sloc: cpp: 119,301; objc: 752; python: 606; ansic: 389; sh: 377; makefile: 44; javascript: 32; xml: 6
file content (83 lines) | stat: -rw-r--r-- 2,976 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * SPDX-FileCopyrightText: 2014 ownCloud GmbH
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#ifndef STRINGUTIL_H
#define STRINGUTIL_H

#pragma once

#include <windows.h>
#include <string>
#include <cassert>

class __declspec(dllexport) StringUtil {
public:
    static std::string  toUtf8(const wchar_t* utf16, int len = -1);
    static std::wstring toUtf16(const char* utf8, int len = -1);

    template<class T>
    static bool begins_with(const T& input, const T& match)
    {
        return input.size() >= match.size()
            && std::equal(match.begin(), match.end(), input.begin());
    }

    static bool isDescendantOf(const std::wstring& child, const std::wstring& parent) {
        return isDescendantOf(child.c_str(), child.size(), parent.c_str(), parent.size());
    }

    static bool isDescendantOf(PCWSTR child, size_t childLength, const std::wstring& parent) {
        return isDescendantOf(child, childLength, parent.c_str(), parent.size());
    }

    static bool isDescendantOf(PCWSTR child, size_t childLength, PCWSTR parent, size_t parentLength) {
        if (!parentLength)
            return false;
        return (childLength == parentLength || childLength > parentLength && (child[parentLength] == L'\\' || child[parentLength - 1] == L'\\'))
            && wcsncmp(child, parent, parentLength) == 0;
    }

    static bool extractChunks(const std::wstring &source, std::wstring &secondChunk, std::wstring &thirdChunk) {
        auto statusBegin = source.find(L':', 0);
        assert(statusBegin != std::wstring::npos);

        auto statusEnd = source.find(L':', statusBegin + 1);
        if (statusEnd == std::wstring::npos) {
            // the command do not contains two colon?
            return false;
        }

        // Assume the caller extracted the chunk before the first colon.
        secondChunk = source.substr(statusBegin + 1, statusEnd - statusBegin - 1);
        thirdChunk = source.substr(statusEnd + 1);
        return true;
    }

    static bool extractChunks(const std::wstring &source, std::wstring &secondChunk, std::wstring &thirdChunk, std::wstring &forthChunk)
    {
        auto statusBegin = source.find(L':', 0);
        assert(statusBegin != std::wstring::npos);

        auto statusEnd = source.find(L':', statusBegin + 1);
        if (statusEnd == std::wstring::npos) {
            // the command do not contains two colon?
            return false;
        }

        auto thirdColon = source.find(L':', statusEnd + 1);
        if (statusEnd == std::wstring::npos) {
            // the command do not contains three colon?
            return false;
        }

        // Assume the caller extracted the chunk before the first colon.
        secondChunk = source.substr(statusBegin + 1, statusEnd - statusBegin - 1);
        thirdChunk = source.substr(statusEnd + 1, thirdColon - statusEnd - 1);
        forthChunk = source.substr(thirdColon + 1);
        return true;
    }
};

#endif // STRINGUTIL_H