Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
/***********************************************************************************************************************************
String Handler Extensions
***********************************************************************************************************************************/
// Include core module
#include "common/type/string.c"
 
#include "harness/string.h"
 
/**********************************************************************************************************************************/
String *
hrnStrUpper(String *const this)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(STRING, this);
    FUNCTION_TEST_END();
 
    ASSERT(this != NULL);
 
    for (size_t idx = 0; idx < strSize(this); idx++)
        this->pub.buffer[idx] = (char)toupper(this->pub.buffer[idx]);
 
    FUNCTION_TEST_RETURN(STRING, this);
}
 
/**********************************************************************************************************************************/
unsigned int
hrnStrReplace(String *const this, const String *const replace, const String *const with)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(STRING, this);
        FUNCTION_TEST_PARAM(STRING, replace);
        FUNCTION_TEST_PARAM(STRING, with);
    FUNCTION_TEST_END();
 
    unsigned int result = 0;
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        // Does replace exist?
        const char *found = strstr(this->pub.buffer, strZ(replace));
 
        while (found != NULL)
        {
            result++;
 
            // Offset into string
            const size_t offset = (size_t)(found - this->pub.buffer);
 
            // Calculate new size and resize
            const size_t size = strSize(this) - strSize(replace) + strSize(with);
            strResize(this, size);
 
            // Replace
            memmove(
                this->pub.buffer + offset + strSize(with), this->pub.buffer + offset + strSize(replace),
                strSize(this) - offset - strSize(replace));
            memcpy(this->pub.buffer + offset, strZ(with), strSize(with));
 
            // Set new size and zero-terminate
            this->pub.size = (unsigned int)size;
            this->pub.buffer[size] = '\0';
 
            // Does replace exist again?
            found = strstr(this->pub.buffer + offset + strSize(with), strZ(replace));
        }
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_TEST_RETURN(UINT, result);
}