-
Notifications
You must be signed in to change notification settings - Fork 687
/
Copy pathstring.cpp
89 lines (82 loc) · 2.34 KB
/
string.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
#include "string.h"
#include "data.h"
#include "module.h"
#include <cstring>
/* PycString */
void PycString::load(PycData* stream, PycModule* mod)
{
if (m_value) delete[] m_value;
if (type() == TYPE_STRINGREF) {
PycRef<PycString> str = mod->getIntern(stream->get32());
m_length = str->length();
if (m_length) {
m_value = new char[m_length+1];
memcpy(m_value, str->value(), m_length);
m_value[m_length] = 0;
} else {
m_value = 0;
}
} else {
m_length = stream->get32();
if (m_length) {
m_value = new char[m_length+1];
stream->getBuffer(m_length, m_value);
m_value[m_length] = 0;
} else {
m_value = 0;
}
if (type() == TYPE_INTERNED)
mod->intern(this);
}
}
bool PycString::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj->type())
return false;
PycRef<PycString> strObj = obj.cast<PycString>();
return isEqual(strObj->m_value);
}
bool PycString::isEqual(const char* str) const
{
if (m_value == str)
return true;
return (strcmp(m_value, str) == 0);
}
void OutputString(PycRef<PycString> str, QuoteStyle style, FILE* F)
{
const char* ch = str->value();
int len = str->length();
if (ch == 0)
return;
while (len--) {
if (*ch < 0x20 || *ch == 0x7F) {
if (*ch == '\r') {
fprintf(F, "\\r");
} else if (*ch == '\n') {
if (style == QS_BlockSingle || style == QS_BlockDouble)
fputc('\n', F);
else
fprintf(F, "\\n");
} else if (*ch == '\t') {
fprintf(F, "\\t");
} else {
fprintf(F, "\\x%x", *ch);
}
} else if (*ch >= 0x80) {
if (str->type() == PycObject::TYPE_UNICODE) {
// Unicode stored as UTF-8... Let the stream interpret it
fputc(*ch, F);
} else {
fprintf(F, "\\x%x", *ch);
}
} else {
if (style == QS_Single && *ch == '\'')
fprintf(F, "\\'");
else if (style == QS_Double && *ch == '"')
fprintf(F, "\\\"");
else
fputc(*ch, F);
}
ch++;
}
}