aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndré Aparício <aparicio99@gmail.com>2012-07-21 02:52:35 +0100
committerPetteri Räty <petsku@petteriraty.eu>2012-08-19 17:52:07 +0300
commitb2f509d72462bbf3dcd89d0164e435556980e3ba (patch)
treed1c06a11be6b391cada86c2251ab26d3cdfc4d5d /src
parentMerge remote-tracking branch 'aparicio/multiline_string_declaration' (diff)
downloadlibbash-b2f509d72462bbf3dcd89d0164e435556980e3ba.tar.gz
libbash-b2f509d72462bbf3dcd89d0164e435556980e3ba.tar.bz2
libbash-b2f509d72462bbf3dcd89d0164e435556980e3ba.zip
Walker: Support string operations on arrays
When a variable expansion applies some string operation like replace on the whole array, apply the operation to every element in the array.
Diffstat (limited to 'src')
-rw-r--r--src/core/interpreter.cpp10
-rw-r--r--src/core/interpreter.h9
2 files changed, 18 insertions, 1 deletions
diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp
index 1b8ef0a..274d01d 100644
--- a/src/core/interpreter.cpp
+++ b/src/core/interpreter.cpp
@@ -244,6 +244,16 @@ std::string interpreter::do_replace_expansion(const std::string& name,
return value;
}
+std::string interpreter::do_array_replace_expansion(const std::string& name,
+ std::function<void(std::string&)> replacer) const
+{
+ std::vector<std::string> array;
+ resolve_array<std::string>(name, array);
+ for(auto i = array.begin(); i != array.end(); ++i)
+ replacer(*i);
+ return boost::algorithm::join(array, resolve<std::string>("IFS").substr(0, 1));
+}
+
std::string::size_type interpreter::get_length(const std::string& name,
const unsigned index) const
{
diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 3c6dde6..0feadef 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -514,7 +514,7 @@ public:
int length) const;
/// \brief perform replacement expansion
- /// \param name the name of the varaible that needs to be expanded
+ /// \param name the name of the variable that needs to be expanded
/// \param replacer the function object used to perform expansion
/// \param index array index, use index=0 if it's not an array
/// \return the expanded value
@@ -522,6 +522,13 @@ public:
std::function<void(std::string&)> replacer,
const unsigned index) const;
+ /// \brief perform array replacement expansion
+ /// \param name the name of the array that needs to be expanded
+ /// \param replacer the function object used to perform expansion
+ /// \return the expanded value
+ std::string do_array_replace_expansion(const std::string& name,
+ std::function<void(std::string&)> replacer) const;
+
/// \brief get the length of a string variable
/// \param name the name of the variable
/// \param index the index of the variable