lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master fb01571 035/156: Pass what is being expanded


From: Greg Chicares
Subject: [lmi-commits] [lmi] master fb01571 035/156: Pass what is being expanded to interpolation function
Date: Tue, 30 Jan 2018 17:22:04 -0500 (EST)

branch: master
commit fb01571cd03d4bd03869ec69337a91da945019b6
Author: Vadim Zeitlin <address@hidden>
Commit: Vadim Zeitlin <address@hidden>

    Pass what is being expanded to interpolation function
    
    Add an extra parameter to the lookup function used by
    interpolate_string() to make it possible distinguish variables from
    sections and from other things in the future.
    
    No real changes yet.
---
 interpolate_string.cpp      | 12 +++++++++---
 interpolate_string.hpp      | 11 ++++++++++-
 interpolate_string_test.cpp | 33 +++++++++++++++++++++++++++++----
 ledger_pdf_generator_wx.cpp |  5 ++++-
 4 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/interpolate_string.cpp b/interpolate_string.cpp
index 0269f78..db3b9d3 100644
--- a/interpolate_string.cpp
+++ b/interpolate_string.cpp
@@ -31,7 +31,7 @@
 
 std::string interpolate_string
     (char const* s
-    ,std::function<std::string (std::string const&)> const& lookup
+    ,lookup_function const& lookup
     )
 {
     std::string out;
@@ -107,7 +107,10 @@ std::string interpolate_string
                             bool active = is_active();
                             if(active)
                                 {
-                                auto const value = lookup(real_name);
+                                auto const value = lookup
+                                    (real_name
+                                    ,interpolate_lookup_kind::section
+                                    );
                                 if(value == "1")
                                     {
                                     active = true;
@@ -176,7 +179,10 @@ std::string interpolate_string
                                 // variable name may seem strange, but why not
                                 // allow using "{{}}" to insert something into
                                 // the interpolated string, after all?
-                                out += lookup(name);
+                                out += lookup
+                                    (name
+                                    ,interpolate_lookup_kind::variable
+                                    );
                                 }
                         }
 
diff --git a/interpolate_string.hpp b/interpolate_string.hpp
index e935a7a..ef2de4d 100644
--- a/interpolate_string.hpp
+++ b/interpolate_string.hpp
@@ -27,6 +27,15 @@
 #include <functional>
 #include <string>
 
+enum class interpolate_lookup_kind
+{
+    variable,
+    section
+};
+
+using lookup_function
+    = std::function<std::string (std::string const&, interpolate_lookup_kind)>;
+
 /// Interpolate string containing embedded variable references.
 ///
 /// Return the input string after replacing all {{variable}} references in it
@@ -50,7 +59,7 @@
 /// Throw if the lookup function throws or if the string uses invalid syntax.
 std::string interpolate_string
     (char const* s
-    ,std::function<std::string (std::string const&)> const& lookup
+    ,lookup_function const& lookup
     );
 
 #endif // interpolate_string_hpp
diff --git a/interpolate_string_test.cpp b/interpolate_string_test.cpp
index e12d931..459a297 100644
--- a/interpolate_string_test.cpp
+++ b/interpolate_string_test.cpp
@@ -29,7 +29,10 @@ int test_main(int, char*[])
 {
     auto const test_interpolate = [](char const* s)
         {
-        return interpolate_string(s, [](std::string const& k) { return k; });
+        return interpolate_string
+            (s
+            ,[](std::string const& k, interpolate_lookup_kind) { return k; }
+            );
         };
 
     // Check that basic interpolation works.
@@ -46,7 +49,7 @@ int test_main(int, char*[])
         {
         return interpolate_string
             (s
-            ,[](std::string const& s) -> std::string
+            ,[](std::string const& s, interpolate_lookup_kind) -> std::string
                 {
                 if(s == "var0") return "0";
                 if(s == "var1") return "1";
@@ -83,7 +86,7 @@ int test_main(int, char*[])
     BOOST_TEST_EQUAL
         (interpolate_string
             ("{{expanded}}"
-            ,[](std::string const& s) -> std::string
+            ,[](std::string const& s, interpolate_lookup_kind) -> std::string
                 {
                 if(s == "expanded")
                     {
@@ -95,6 +98,28 @@ int test_main(int, char*[])
         ,"{{unexpanded}}"
         );
 
+    // Check that the kind of variable being expanded is correct.
+    BOOST_TEST_EQUAL
+        (interpolate_string
+            ("{{#section1}}{{^section0}}{{variable}}{{/section0}}{{/section1}}"
+            ,[](std::string const& s, interpolate_lookup_kind kind)
+                {
+                switch(kind)
+                    {
+                    case interpolate_lookup_kind::variable:
+                        return "value of " + s;
+
+                    case interpolate_lookup_kind::section:
+                        // Get rid of the "section" prefix.
+                        return s.substr(7);
+                    }
+
+                throw std::runtime_error("invalid lookup kind");
+                }
+            )
+        ,"value of variable"
+        );
+
     // Should throw if the input syntax is invalid.
     BOOST_TEST_THROW
         (test_interpolate("{{x")
@@ -131,7 +156,7 @@ int test_main(int, char*[])
     BOOST_TEST_THROW
         (interpolate_string
             ("{{x}}"
-            ,[](std::string const& s) -> std::string
+            ,[](std::string const& s, interpolate_lookup_kind) -> std::string
                 {
                 throw std::runtime_error("no such variable '" + s + "'");
                 }
diff --git a/ledger_pdf_generator_wx.cpp b/ledger_pdf_generator_wx.cpp
index bb7987f..d258771 100644
--- a/ledger_pdf_generator_wx.cpp
+++ b/ledger_pdf_generator_wx.cpp
@@ -91,7 +91,10 @@ class html_interpolator
         return text::from_html
             (interpolate_string
                 (s
-                ,[this](std::string const& s) { return 
expand_html(s).as_html(); }
+                ,[this](std::string const& s, interpolate_lookup_kind)
+                    {
+                    return expand_html(s).as_html();
+                    }
                 )
             );
     }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]