From: "Petteri Räty" <betelgeuse@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/, src/core/tests/
Date: Wed, 6 Apr 2011 15:07:11 +0000 (UTC) [thread overview]
Message-ID: <5e4a93528fc7b0cfef25f7265b1c308b4a54fded.betelgeuse@gentoo> (raw)
commit: 5e4a93528fc7b0cfef25f7265b1c308b4a54fded
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Tue Apr 5 13:29:44 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Wed Apr 6 10:07:49 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=5e4a9352
Implement substring expansion
Support substring expansion with string variables. Arrays and special
variables like @, * are not supported yet.
---
bashast/libbashWalker.g | 11 ++++++++-
scripts/var_expansion.ebuild | 13 +++++++++++
scripts/var_expansion.ebuild.result | 13 +++++++++++
src/core/interpreter.h | 41 +++++++++++++++++++++++++++++++++++
src/core/tests/interpreter_test.cpp | 6 +++++
5 files changed, 83 insertions(+), 1 deletions(-)
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index ad8e444..e6f4393 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -86,7 +86,10 @@ var_name returns[std::string libbash_value]
}:
num|name|TIMES|AT;
-var_expansion returns[std::string libbash_value]:
+var_expansion returns[std::string libbash_value]
+@declarations {
+ bool use_length;
+}:
^(USE_DEFAULT var_name libbash_word=word) {
libbash_value = walker->do_default_expansion($var_name.libbash_value, libbash_word);
}
@@ -95,6 +98,12 @@ var_expansion returns[std::string libbash_value]:
}
|^(USE_ALTERNATE var_name libbash_word=word) {
libbash_value = walker->do_alternate_expansion($var_name.libbash_value, libbash_word);
+ }
+ |^(OFFSET var_name offset=arithmetics { use_length = false; } (length=arithmetics { use_length = true; })?) {
+ if(use_length)
+ libbash_value = walker->do_substring_expansion($var_name.libbash_value, offset, length);
+ else
+ libbash_value = walker->do_substring_expansion($var_name.libbash_value, offset);
};
word returns[std::string libbash_value]:
diff --git a/scripts/var_expansion.ebuild b/scripts/var_expansion.ebuild
index f0ad504..247f512 100644
--- a/scripts/var_expansion.ebuild
+++ b/scripts/var_expansion.ebuild
@@ -9,3 +9,16 @@ FOO6=${EAPI:=hello}
FOO7=${FOO8:=hello}
FOO9=${EAPI:+hello}
FOO10=${NOT_EXIST:+hello}
+FOO11=${FOO9:0}
+FOO12=${FOO9:2}
+FOO13=${FOO9: -2}
+FOO14=${FOO9:100}
+FOO15=${FOO9: -100}
+FOO16=${FOO9:(-5 + 5)}
+FOO17=${NOT_EXIST:0}
+FOO18=${FOO9:0:2}
+FOO19=${FOO9:2:2}
+FOO20=${FOO9: -2:2}
+FOO21=${FOO9:2:100}
+FOO22=${FOO9: -2:100}
+FOO23=${NOT_EXIST:0:2}
diff --git a/scripts/var_expansion.ebuild.result b/scripts/var_expansion.ebuild.result
index 7d2e5ca..2c5c42f 100644
--- a/scripts/var_expansion.ebuild.result
+++ b/scripts/var_expansion.ebuild.result
@@ -2,7 +2,20 @@ EAPI=3
EAPI4=4
FOO=3
FOO10=
+FOO11=hello
+FOO12=llo
+FOO13=lo
+FOO14=
+FOO15=
+FOO16=hello
+FOO17=
+FOO18=he
+FOO19=ll
FOO2=hello
+FOO20=lo
+FOO21=llo
+FOO22=lo
+FOO23=
FOO3=123
FOO4=3
FOO5=2
diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index aad8f48..cde08ac 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -44,6 +44,18 @@ class interpreter{
/// \var private::members
/// \brief global symbol table
scope members;
+
+ /// \brief calculate the correct offset when offset < 0 and check whether
+ /// the real offset is in legal range
+ /// \param[in,out] a value/result argument referring to offset
+ /// \param[in] the original string
+ /// \return whether the real offset is in legal range
+ bool get_real_offset(int& offset, const std::string& str)
+ {
+ offset = (offset >= 0? offset : str.size() + offset);
+ return !(offset < 0 || offset >= static_cast<int>(str.size()));
+ }
+
public:
///
@@ -444,5 +456,34 @@ public:
{
return (is_unset_or_null(name)? "" : value);
}
+
+ /// \brief perform substring expansion
+ /// \param the offset of the substring
+ /// \return the expansion result
+ const std::string do_substring_expansion(const std::string& name,
+ int offset)
+ {
+ std::string value = resolve<std::string>(name);
+ if(!get_real_offset(offset, value))
+ return "";
+ return value.substr(offset);
+ }
+
+ /// \brief perform substring expansion
+ /// \param the offset of the substring
+ /// \param the length of the substring
+ /// \return the expansion result
+ const std::string do_substring_expansion(const std::string& name,
+ int offset,
+ int length)
+ {
+ if(length < 0)
+ throw interpreter_exception("length of substring expression should be greater or equal to zero");
+ std::string value = resolve<std::string>(name);
+ if(!get_real_offset(offset, value))
+ return "";
+ return value.substr(offset, length);
+ }
+
};
#endif
diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpreter_test.cpp
index 2d46e62..6cc2df9 100644
--- a/src/core/tests/interpreter_test.cpp
+++ b/src/core/tests/interpreter_test.cpp
@@ -90,3 +90,9 @@ TEST(interpreter, set_string_value)
interpreter_exception);
EXPECT_STREQ("hi", walker.resolve<string>("astring_ro").c_str());
}
+
+TEST(interperter, substring_expansion_exception)
+{
+ interpreter walker;
+ EXPECT_THROW(walker.do_substring_expansion("", 0, -1), interpreter_exception);
+}
next reply other threads:[~2011-04-06 15:07 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-06 15:07 Petteri Räty [this message]
-- strict thread matches above, loose matches on Subject: below --
2011-04-14 4:50 [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/, src/core/tests/ Petteri Räty
2011-04-28 6:19 Petteri Räty
2011-05-22 21:00 Petteri Räty
2011-05-23 14:34 Petteri Räty
2011-06-03 12:43 Petteri Räty
2012-06-03 9:08 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=5e4a93528fc7b0cfef25f7265b1c308b4a54fded.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