1 |
commit: eccb906c8fd1df61fc1f554485e560bcb4f8d1eb |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Tue Apr 12 01:07:45 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Apr 12 07:22:49 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=eccb906c |
7 |
|
8 |
Implement get_length and get_array_length for variable |
9 |
|
10 |
get_length can use one optional argument to specify array index. |
11 |
get_array_length returns the internal map size. |
12 |
|
13 |
--- |
14 |
src/core/interpreter.h | 7 +++++-- |
15 |
src/core/symbols.hpp | 15 +++++++++++++++ |
16 |
src/core/tests/symbols_test.cpp | 14 ++++++++++++++ |
17 |
3 files changed, 34 insertions(+), 2 deletions(-) |
18 |
|
19 |
diff --git a/src/core/interpreter.h b/src/core/interpreter.h |
20 |
index 7155639..43f816b 100644 |
21 |
--- a/src/core/interpreter.h |
22 |
+++ b/src/core/interpreter.h |
23 |
@@ -492,9 +492,12 @@ public: |
24 |
/// \brief get the length of a string variable |
25 |
/// \param the name of the variable |
26 |
/// \return the length |
27 |
- int get_length(const std::string& name) |
28 |
+ unsigned get_length(const std::string& name, const unsigned index=0) |
29 |
{ |
30 |
- return resolve<std::string>(name).size(); |
31 |
+ std::shared_ptr<variable> value = members.resolve(name); |
32 |
+ if(!value) |
33 |
+ return 0; |
34 |
+ return value->get_length(index); |
35 |
} |
36 |
|
37 |
}; |
38 |
|
39 |
diff --git a/src/core/symbols.hpp b/src/core/symbols.hpp |
40 |
index f645047..9bb0b98 100644 |
41 |
--- a/src/core/symbols.hpp |
42 |
+++ b/src/core/symbols.hpp |
43 |
@@ -178,6 +178,21 @@ public: |
44 |
value[index] = new_value; |
45 |
} |
46 |
|
47 |
+ /// \brief get the length of a variable |
48 |
+ /// \param the index of the variable, use 0 if it's not an array |
49 |
+ /// \return the length of the variable |
50 |
+ unsigned get_length(const unsigned index=0) const |
51 |
+ { |
52 |
+ return get_value<std::string>(index).size(); |
53 |
+ } |
54 |
+ |
55 |
+ /// \brief get the length of an array variable |
56 |
+ /// \return the length of the array |
57 |
+ unsigned get_array_length() const |
58 |
+ { |
59 |
+ return value.size(); |
60 |
+ } |
61 |
+ |
62 |
/// \brief check whether the value of the variable is null |
63 |
/// \return whether the value of the variable is null |
64 |
bool is_null() const |
65 |
|
66 |
diff --git a/src/core/tests/symbols_test.cpp b/src/core/tests/symbols_test.cpp |
67 |
index 2b7a65e..1602f52 100644 |
68 |
--- a/src/core/tests/symbols_test.cpp |
69 |
+++ b/src/core/tests/symbols_test.cpp |
70 |
@@ -103,6 +103,20 @@ TEST(symbol_test, is_null) |
71 |
EXPECT_TRUE(variable("foo", "", false, true).is_null()); |
72 |
} |
73 |
|
74 |
+TEST(symbol_test, get_length) |
75 |
+{ |
76 |
+ variable an_int("foo", 10); |
77 |
+ EXPECT_EQ(2, an_int.get_length()); |
78 |
+ |
79 |
+ variable an_string("bar", "hello world"); |
80 |
+ EXPECT_EQ(11, an_string.get_length()); |
81 |
+ |
82 |
+ map<int, string> values = {{0, "1"}, {1, "2"}, {2, "hello"}}; |
83 |
+ variable array("array", values); |
84 |
+ EXPECT_EQ(5, array.get_length(2)); |
85 |
+ EXPECT_EQ(3, array.get_array_length()); |
86 |
+} |
87 |
+ |
88 |
TEST(scope_test, define_resolve) |
89 |
{ |
90 |
scope members; |