From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1Q9iLV-0007DU-QJ for garchives@archives.gentoo.org; Tue, 12 Apr 2011 18:29:50 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id D01901C063; Tue, 12 Apr 2011 18:29:22 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 900BB1C064 for ; Tue, 12 Apr 2011 18:29:22 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 1D2611BC0CF for ; Tue, 12 Apr 2011 18:29:22 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 715EB8006D for ; Tue, 12 Apr 2011 18:29:21 +0000 (UTC) From: "Petteri Räty" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Petteri Räty" Message-ID: Subject: [gentoo-commits] proj/libbash:master commit in: src/core/, src/core/tests/ X-VCS-Repository: proj/libbash X-VCS-Files: src/core/interpreter.h src/core/symbols.hpp src/core/tests/symbols_test.cpp X-VCS-Directories: src/core/ src/core/tests/ X-VCS-Committer: betelgeuse X-VCS-Committer-Name: Petteri Räty X-VCS-Revision: eccb906c8fd1df61fc1f554485e560bcb4f8d1eb Date: Tue, 12 Apr 2011 18:29:21 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 3e3a3ce85cf57fe6900385900e14c248 commit: eccb906c8fd1df61fc1f554485e560bcb4f8d1eb Author: Mu Qiao gentoo org> AuthorDate: Tue Apr 12 01:07:45 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Tue Apr 12 07:22:49 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3Deccb906c Implement get_length and get_array_length for variable get_length can use one optional argument to specify array index. get_array_length returns the internal map size. --- src/core/interpreter.h | 7 +++++-- src/core/symbols.hpp | 15 +++++++++++++++ src/core/tests/symbols_test.cpp | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/core/interpreter.h b/src/core/interpreter.h index 7155639..43f816b 100644 --- a/src/core/interpreter.h +++ b/src/core/interpreter.h @@ -492,9 +492,12 @@ public: /// \brief get the length of a string variable /// \param the name of the variable /// \return the length - int get_length(const std::string& name) + unsigned get_length(const std::string& name, const unsigned index=3D0) { - return resolve(name).size(); + std::shared_ptr value =3D members.resolve(name); + if(!value) + return 0; + return value->get_length(index); } =20 }; diff --git a/src/core/symbols.hpp b/src/core/symbols.hpp index f645047..9bb0b98 100644 --- a/src/core/symbols.hpp +++ b/src/core/symbols.hpp @@ -178,6 +178,21 @@ public: value[index] =3D new_value; } =20 + /// \brief get the length of a variable + /// \param the index of the variable, use 0 if it's not an array + /// \return the length of the variable + unsigned get_length(const unsigned index=3D0) const + { + return get_value(index).size(); + } + + /// \brief get the length of an array variable + /// \return the length of the array + unsigned get_array_length() const + { + return value.size(); + } + /// \brief check whether the value of the variable is null /// \return whether the value of the variable is null bool is_null() const diff --git a/src/core/tests/symbols_test.cpp b/src/core/tests/symbols_tes= t.cpp index 2b7a65e..1602f52 100644 --- a/src/core/tests/symbols_test.cpp +++ b/src/core/tests/symbols_test.cpp @@ -103,6 +103,20 @@ TEST(symbol_test, is_null) EXPECT_TRUE(variable("foo", "", false, true).is_null()); } =20 +TEST(symbol_test, get_length) +{ + variable an_int("foo", 10); + EXPECT_EQ(2, an_int.get_length()); + + variable an_string("bar", "hello world"); + EXPECT_EQ(11, an_string.get_length()); + + map values =3D {{0, "1"}, {1, "2"}, {2, "hello"}}; + variable array("array", values); + EXPECT_EQ(5, array.get_length(2)); + EXPECT_EQ(3, array.get_array_length()); +} + TEST(scope_test, define_resolve) { scope members;