Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/mednadisc/string/trim.cpp
2 views
1
/* Mednafen - Multi-system Emulator
2
*
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*/
17
18
#include <string>
19
#include "emuware/emuware.h"
20
#include "trim.h"
21
22
// Remove whitespace from beginning of string
23
void MDFN_ltrim(char *string)
24
{
25
int32 di, si;
26
bool InWhitespace = true;
27
28
di = si = 0;
29
30
while(string[si])
31
{
32
if(InWhitespace && (string[si] == ' ' || string[si] == '\r' || string[si] == '\n' || string[si] == '\t' || string[si] == 0x0b))
33
{
34
35
}
36
else
37
{
38
InWhitespace = false;
39
string[di] = string[si];
40
di++;
41
}
42
si++;
43
}
44
string[di] = 0;
45
}
46
47
// Remove whitespace from end of string
48
void MDFN_rtrim(char *string)
49
{
50
int32 len = strlen(string);
51
52
if(len)
53
{
54
for(int32 x = len - 1; x >= 0; x--)
55
{
56
if(string[x] == ' ' || string[x] == '\r' || string[x] == '\n' || string[x] == '\t' || string[x] == 0x0b)
57
string[x] = 0;
58
else
59
break;
60
}
61
}
62
63
}
64
65
void MDFN_trim(char *string)
66
{
67
MDFN_rtrim(string);
68
MDFN_ltrim(string);
69
}
70
71
72
// Remove whitespace from beginning of string
73
void MDFN_ltrim(std::string &string)
74
{
75
size_t len = string.length();
76
size_t di, si;
77
bool InWhitespace = true;
78
79
di = si = 0;
80
81
while(si < len)
82
{
83
if(InWhitespace && (string[si] == ' ' || string[si] == '\r' || string[si] == '\n' || string[si] == '\t' || string[si] == 0x0b))
84
{
85
86
}
87
else
88
{
89
InWhitespace = false;
90
string[di] = string[si];
91
di++;
92
}
93
si++;
94
}
95
96
string.resize(di);
97
}
98
99
// Remove whitespace from end of string
100
void MDFN_rtrim(std::string &string)
101
{
102
size_t len = string.length();
103
104
if(len)
105
{
106
size_t x = len;
107
size_t new_len = len;
108
109
do
110
{
111
x--;
112
113
if(!(string[x] == ' ' || string[x] == '\r' || string[x] == '\n' || string[x] == '\t' || string[x] == 0x0b))
114
break;
115
116
new_len--;
117
} while(x);
118
119
string.resize(new_len);
120
}
121
}
122
123
124
void MDFN_trim(std::string &string)
125
{
126
MDFN_rtrim(string);
127
MDFN_ltrim(string);
128
}
129
130
131
char *MDFN_RemoveControlChars(char *str)
132
{
133
char *orig = str;
134
135
if(str)
136
{
137
while(*str)
138
{
139
if((unsigned char)*str < 0x20)
140
*str = 0x20;
141
str++;
142
}
143
}
144
145
return(orig);
146
}
147
148
149