-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize_string.cpp
More file actions
62 lines (46 loc) · 1.52 KB
/
Copy pathnormalize_string.cpp
File metadata and controls
62 lines (46 loc) · 1.52 KB
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
/*
* normalize_string.cpp
*
* Created on: Nov 15, 2014
* Author: edwardh
*/
#include "string.h"
#include "normalize_string.h"
unsigned int Normalizer::normalize_string_inplace(char * string, size_t& string_len, const char norm_char)
{
size_t normalized_string_len = string_len;
for(char *str_itr = string; str_itr < &string[normalized_string_len-1]; ++str_itr)
{
if(norm_char == *str_itr)
{
char *dst;
for(dst = ++str_itr; (norm_char == *str_itr); ++str_itr);
if(str_itr > dst)
{
memmove(dst, str_itr, normalized_string_len - (str_itr - string));
normalized_string_len -= str_itr - dst;
}
}
}
int num_of_reduced_appearances = string_len - normalized_string_len;
string_len = normalized_string_len;
return num_of_reduced_appearances;
}
unsigned int Normalizer::normalize_string_fast(const char *src_string, char *dst_string, size_t& string_len, const char norm_char)
{
const char *in_itr = src_string;
char *out_itr = dst_string;
while(in_itr < &src_string[string_len-1])
{
*out_itr = *in_itr;
if(norm_char == *in_itr)
for(++in_itr; (norm_char == *in_itr); ++in_itr);
else
++in_itr;
++out_itr;
}
string_len = out_itr - dst_string;
size_t src_string_len = in_itr - src_string;
size_t num_of_reduced_appearances = src_string_len - string_len;
return num_of_reduced_appearances;
}