File: StringUtils.cpp

package info (click to toggle)
openrgb 0.9%2Bgit20251009%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,304 kB
  • sloc: cpp: 197,011; ansic: 1,290; sh: 402; xml: 71; python: 65; makefile: 13
file content (84 lines) | stat: -rw-r--r-- 2,487 bytes parent folder | download
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
84
/*---------------------------------------------------------*\
| StringUtils.cpp                                           |
|                                                           |
|   String utility functions                                |
|                                                           |
|   This file is part of the OpenRGB project                |
|   SPDX-License-Identifier: GPL-2.0-or-later               |
\*---------------------------------------------------------*/

#include <codecvt>
#include <locale>
#include <string>
#include "StringUtils.h"

const char* StringUtils::wchar_to_char(const wchar_t* pwchar)
{
    if(pwchar == nullptr)
    {
        return "";
    }

    /*-----------------------------------------------------*\
    | Get the number of characters in the string.           |
    \*-----------------------------------------------------*/
    int currentCharIndex = 0;
    char currentChar = (char)pwchar[currentCharIndex];

    while(currentChar != '\0')
    {
        currentCharIndex++;
        currentChar = (char)pwchar[currentCharIndex];
    }

    const int charCount = currentCharIndex + 1;

    /*-----------------------------------------------------*\
    | Allocate a new block of memory size char (1 byte)     |
    | instead of wide char (2 bytes)                        |
    \*-----------------------------------------------------*/
    char* filePathC = (char*)malloc(sizeof(char) * charCount);

    for(int i = 0; i < charCount; i++)
    {
        /*-------------------------------------------------*\
        | Convert to char (1 byte)                          |
        \*-------------------------------------------------*/
        char character = (char)pwchar[i];

        *filePathC = character;

        filePathC += sizeof(char);

    }

    filePathC += '\0';

    filePathC -= (sizeof(char) * charCount);

    return(filePathC);
}

std::string StringUtils::wstring_to_string(const std::wstring wstring)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;

    return(converter.to_bytes(wstring));
}

std::string StringUtils::u16string_to_string(const std::u16string wstring)
{
    std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> converter;

    return(converter.to_bytes(wstring));
}

const std::string StringUtils::remove_null_terminating_chars(std::string input)
{
    while(!input.empty() && input.back() == 0)
    {
        input.pop_back();
    }

    return(input);
}