~aleteoryx/muditaos

ref: 5ad878c6c49357da4494e7809b9a026ad662fb85 muditaos/module-utils/ucs2/UCS2.hpp -rw-r--r-- 2.1 KiB
5ad878c6 — Paweł Joński [BH-371] Replace gsl with mainline submodule and move to third-party 4 years ago
                                                                                
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <cstdint>
#include <memory>
#include "utf8/UTF8.hpp"

class UCS2
{
  private:
    // number of characters in the string. its equal to size of allocated memory plus null terminator
    uint32_t length = 0U;
    // size in bytes of memory used in buffer
    uint32_t sizeUsed = 0U;
    // size in bytes of memory that was allcated to the buffer
    uint32_t sizeAllocated = 0U;
    // pointer to memory where ucs2 characters are stored.
    std::unique_ptr<uint32_t[]> buffer;
    void clear();
    [[nodiscard]] uint32_t convertFromUtf(uint32_t utfChar) const noexcept;

    [[nodiscard]] std::string inline convertToUtf8String(const std::u32string &s) const;
    [[nodiscard]] uint32_t getUcs2Char(const std::string &string, const std::size_t &i);

  public:
    UCS2();
    /*
     * @brief Initializes new ucs2 string from utf8 string. It's used to convert text from
     * utf8 to ucs2.
     * @param string utf8 string to convert
     */
    explicit UCS2(const UTF8 &string);
    /*
     * @brief Initializes new ucs2 string from std::string. It's used to convert text from
     * modem message format to ucs2.
     * @param string std::string to convert
     */
    explicit UCS2(const std::string &string);
    explicit UCS2(UCS2 &ucs);
    [[nodiscard]] const char *c_str() const noexcept
    {
        return reinterpret_cast<const char *>(buffer.get());
    }
    /*
     * @brief It's converting ucs2 to utf string.
     * @return utf8 string
     */
    [[nodiscard]] UTF8 toUTF8() const noexcept;
    void append(const uint32_t &ucs2char);
    /*
     * @brief It's converting text coded in ucs2 to string. Used to send data to modem.
     * @return coded string
     */
    [[nodiscard]] std::string str() const noexcept;
    [[nodiscard]] uint32_t getLength() const noexcept
    {
        return length;
    };
    [[nodiscard]] uint32_t getSizeUsed() const noexcept
    {
        return sizeUsed;
    };
    [[nodiscard]] uint32_t getSizeAlocated() const noexcept
    {
        return sizeAllocated;
    };
};