public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Petteri Räty" <betelgeuse@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/libbash:master commit in: src/core/, src/core/tests/
Date: Tue, 12 Apr 2011 18:29:21 +0000 (UTC)	[thread overview]
Message-ID: <eccb906c8fd1df61fc1f554485e560bcb4f8d1eb.betelgeuse@gentoo> (raw)

commit:     eccb906c8fd1df61fc1f554485e560bcb4f8d1eb
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Tue Apr 12 01:07:45 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Tue Apr 12 07:22:49 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=eccb906c

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=0)
   {
-    return resolve<std::string>(name).size();
+    std::shared_ptr<variable> value = members.resolve(name);
+    if(!value)
+      return 0;
+    return value->get_length(index);
   }
 
 };

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] = new_value;
   }
 
+  /// \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=0) const
+  {
+    return get_value<std::string>(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_test.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());
 }
 
+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<int, string> values = {{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;



             reply	other threads:[~2011-04-12 18:29 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-12 18:29 Petteri Räty [this message]
  -- strict thread matches above, loose matches on Subject: below --
2011-05-27 23:03 [gentoo-commits] proj/libbash:master commit in: src/core/, src/core/tests/ Petteri Räty
2011-05-25 19:42 Petteri Räty
2011-05-23 14:34 Petteri Räty
2011-05-23 14:34 Petteri Räty
2011-05-08 13:07 Petteri Räty
2011-04-27 15:11 Petteri Räty
2011-04-14  4:50 Petteri Räty
2011-04-14  4:50 Petteri Räty
2011-04-12 18:29 Petteri Räty
2011-04-12 18:29 Petteri Räty
2011-04-06  7:43 Petteri Räty
2011-04-04 16:09 Petteri Räty
2011-04-04 15:52 Petteri Räty
2011-04-04 15:52 Petteri Räty
2011-03-30 12:48 Petteri Räty

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=eccb906c8fd1df61fc1f554485e560bcb4f8d1eb.betelgeuse@gentoo \
    --to=betelgeuse@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox