-
Notifications
You must be signed in to change notification settings - Fork 3
/
sexpression_util.cc
86 lines (80 loc) · 2.73 KB
/
sexpression_util.cc
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
// Copyright 2007 Google Inc. All Rights Reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// Author: [email protected] (Stephen Chen)
#include "sexpression_util.h"
#include "sexpression.h"
// Given an S-Expression in the form of ((key1 value1) (key2 value2) ...)
// and a key, return the SExpression value associated with the key.
const SExpression* SExpressionAssocGet(const SExpression* sexpr,
const string& key) {
if (!sexpr->IsList() || sexpr->IsNil()) {
return NULL;
}
SExpression::const_iterator i = sexpr->Begin();
while (i != sexpr->End()) {
if (!i->IsList()) {
++i;
continue;
}
// if i is a list, check the first element against the key.
SExpression::const_iterator j = i->Begin();
if (j != i->End() && j->IsSymbol()) {
const SExpressionSymbol* symbol =
down_cast<const SExpressionSymbol*>(&(*j));
if (symbol->name() == key) {
++j;
return &(*j);
}
}
++i;
}
return NULL;
}
string SExpressionAssocReplace(const SExpression* sexpr, const string& key,
const string& value) {
if (!sexpr->IsList() || sexpr->IsNil()) {
return sexpr->Repr();
}
string output = "(";
for (SExpression::const_iterator i = sexpr->Begin(); i != sexpr->End();
++i) {
if (!i->IsList()) {
output.append(i->Repr());
output.append(" ");
continue;
}
// If i is a list, check the first element against the key.
SExpression::const_iterator j = i->Begin();
if (j != i->End() && j->IsSymbol()) {
const SExpressionSymbol* symbol =
down_cast<const SExpressionSymbol*>(&(*j));
if (symbol->name() == key) { // Found key, do substitution.
output.append("(");
output.append(key);
output.append(" ");
output.append(value);
output.append(") ");
continue;
}
}
// If i was a list or it didn't match the key, add i to output.
output.append(i->Repr());
output.append(" ");
}
output.append(")");
return output;
}