1
|
|
/*
|
2
|
|
*******************************************************************************
|
3
|
|
\file str.c
|
4
|
|
\brief Strings
|
5
|
|
\project bee2 [cryptographic library]
|
6
|
|
\author (C) Sergey Agievich [agievich@{bsu.by|gmail.com}]
|
7
|
|
\created 2013.02.04
|
8
|
|
\version 2016.09.19
|
9
|
|
\license This program is released under the GNU General Public License
|
10
|
|
version 3. See Copyright Notices in bee2/info.h.
|
11
|
|
*******************************************************************************
|
12
|
|
*/
|
13
|
|
|
14
|
|
#include "bee2/core/mem.h"
|
15
|
|
#include "bee2/core/str.h"
|
16
|
|
#include "bee2/core/util.h"
|
17
|
|
|
18
|
|
/*
|
19
|
|
*******************************************************************************
|
20
|
|
Характеристики / проверка
|
21
|
|
|
22
|
|
\warning В strLen() нельзя вызывать strIsValid() -- будет рекурсия.
|
23
|
|
*******************************************************************************
|
24
|
|
*/
|
25
|
|
|
26
|
1
|
size_t strLen(const char* str)
|
27
|
|
{
|
28
|
1
|
return str ? strlen(str) : SIZE_0;
|
29
|
|
}
|
30
|
|
|
31
|
1
|
size_t strLen2(const char* str, size_t count)
|
32
|
|
{
|
33
|
1
|
ASSERT(strIsValid(str));
|
34
|
1
|
return str ? strnlen(str, count) : SIZE_0;
|
35
|
|
}
|
36
|
|
|
37
|
1
|
bool_t strIsValid(const char* str)
|
38
|
|
{
|
39
|
1
|
return memIsValid(str, strLen(str) + (str ? 1 : 0));
|
40
|
|
}
|
41
|
|
|
42
|
|
/*
|
43
|
|
*******************************************************************************
|
44
|
|
Стандартные функции
|
45
|
|
|
46
|
|
\remark strLen() реализована через memcpy(), а не через strcpy(),
|
47
|
|
чтобы избежать предупреждений MSVC.
|
48
|
|
*******************************************************************************
|
49
|
|
*/
|
50
|
|
|
51
|
1
|
void strCopy(char* dest, const char* src)
|
52
|
|
{
|
53
|
1
|
ASSERT(strIsValid(src));
|
54
|
1
|
ASSERT(memIsValid(dest, strLen(src) + 1));
|
55
|
1
|
ASSERT(memIsDisjoint(src, dest, strLen(src) + 1));
|
56
|
1
|
memcpy(dest, src, strLen(src) + 1);
|
57
|
|
}
|
58
|
|
|
59
|
1
|
int strCmp(const char* str1, const char* str2)
|
60
|
|
{
|
61
|
1
|
ASSERT(strIsValid(str1));
|
62
|
1
|
ASSERT(strIsValid(str2));
|
63
|
1
|
return strcmp(str1, str2);
|
64
|
|
}
|
65
|
|
|
66
|
|
/*
|
67
|
|
*******************************************************************************
|
68
|
|
Структура
|
69
|
|
*******************************************************************************
|
70
|
|
*/
|
71
|
|
|
72
|
1
|
bool_t strIsAlphanumeric(const char* str)
|
73
|
|
{
|
74
|
1
|
ASSERT(strIsValid(str));
|
75
|
1
|
for (; *str; ++str)
|
76
|
1
|
if ((*str < '0' || *str > '9') &&
|
77
|
1
|
(*str < 'A' || *str > 'Z') &&
|
78
|
1
|
(*str < 'a' || *str > 'z'))
|
79
|
1
|
return FALSE;
|
80
|
1
|
return TRUE;
|
81
|
|
}
|
82
|
|
|
83
|
1
|
bool_t strStartsWith(const char* str, const char* prefix)
|
84
|
|
{
|
85
|
1
|
ASSERT(strIsValid(str));
|
86
|
1
|
ASSERT(strIsValid(prefix));
|
87
|
1
|
for(; *prefix; ++prefix, ++str)
|
88
|
1
|
if (*str != *prefix)
|
89
|
1
|
return FALSE;
|
90
|
1
|
return TRUE;
|
91
|
|
}
|
92
|
|
|
93
|
1
|
bool_t strEndsWith(const char* str, const char* suffix)
|
94
|
|
{
|
95
|
1
|
ASSERT(strIsValid(str));
|
96
|
1
|
ASSERT(strIsValid(suffix));
|
97
|
1
|
if (strLen(str) < strLen(suffix))
|
98
|
1
|
return FALSE;
|
99
|
1
|
for(str += strLen(str) - strLen(suffix); *suffix; ++suffix, ++str)
|
100
|
1
|
if (*str != *suffix)
|
101
|
1
|
return FALSE;
|
102
|
1
|
return TRUE;
|
103
|
|
}
|
104
|
|
|
105
|
|
/*
|
106
|
|
*******************************************************************************
|
107
|
|
Операции
|
108
|
|
*******************************************************************************
|
109
|
|
*/
|
110
|
|
|
111
|
1
|
void strRev(char* str)
|
112
|
|
{
|
113
|
|
size_t i, j;
|
114
|
1
|
ASSERT(strIsValid(str));
|
115
|
1
|
for (i = 0, j = strLen(str); i < j;)
|
116
|
|
{
|
117
|
1
|
str[i] ^= str[--j];
|
118
|
1
|
str[j] ^= str[i];
|
119
|
1
|
str[i++] ^= str[j];
|
120
|
|
}
|
121
|
|
}
|