-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringtable.cpp
108 lines (102 loc) · 2.86 KB
/
stringtable.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
////////////////////////////////////////////////////////////////////////////////
// DScript Scripting Language
// Copyright (C) 2003 Bryan "daerid" Ross
//
// Permission to copy, use, modify, sell and distribute this software is
// granted provided this copyright notice appears in all copies. This
// software is provided "as is" without express or implied warranty, and
// with no claim as to its suitability for any purpose.
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// DScript Includes
#include "stringtable.h"
////////////////////////////////////////////////////////////////////////////////
using namespace std;
using namespace dscript;
string_table::entry string_table::insert(const string& val)
{
typedef set<string>::const_iterator iter_t;
typedef pair<iter_t,bool> ret_t;
ret_t r = m_strings.insert(val);
return r.first->c_str();
}
string_table::entry string_table::find(const string& val)
{
typedef set<string>::const_iterator c_iter;
c_iter found = m_strings.find(val);
if(found == m_strings.end())
return 0;
else
return found->c_str();
}
string dscript::escape(const string& str)
{
typedef string::const_iterator iter_t;
iter_t st = str.begin(); // skip beginning "
iter_t en = str.end(); // skip ending "
// not the fastest way of doing it, but it works
string res;
for(; st != en; ++st)
{
switch(*st)
{
case '\n':
res.append("\\n");
break;
case '\t':
res.append("\\t");
break;
case '\r':
res.append("\\r");
break;
case '\f':
res.append("\\f");
break;
case '"':
res.append("\\\"");
break;
default:
res.push_back(*st);
}
}
return res;
}
string dscript::unescape(const string& str)
{
typedef string::const_iterator iter_t;
iter_t st = str.begin() + 1; // skip beginning "
iter_t en = str.end() - 1; // skip ending "
// not the fastest way of doing it, but it works
string res;
for(; st != en; ++st)
{
if(*st == '\\')
{
// check next
++st;
switch(*st)
{
case 'n':
res.push_back('\n');
break;
case 't':
res.push_back('\t');
break;
case 'r':
res.push_back('\r');
break;
case 'f':
res.push_back('\f');
break;
case '"':
res.push_back('"');
break;
default:
res.push_back(*st);
}
}
else
res.push_back(*st);
}
return res;
}