Skip to content

Commit 62daf9c

Browse files
author
Kasper Peeters
committed
Fix handling of quotes inside dollar expressions (issue #220).
1 parent 1d14d50 commit 62daf9c

File tree

5 files changed

+172
-4
lines changed

5 files changed

+172
-4
lines changed

core/CdbPython.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#include "DataCell.hh"
1616
#endif
1717

18-
// #define DEBUG
18+
// #define DEBUG __FILE__
19+
#include "Debug.hh"
1920

2021
std::string cadabra::escape_quotes(const std::string& line)
2122
{
@@ -467,11 +468,11 @@ std::string replace_dollar_expressions(const std::string& input,
467468
char c = input[i];
468469

469470
// Toggle quote state
470-
if (c == '"' && !in_single_quote) {
471+
if (c == '"' && !in_single_quote && dollar_start == std::string::npos) {
471472
in_double_quote = !in_double_quote;
472473
result << c;
473474
}
474-
else if (c == '\'' && !in_double_quote) {
475+
else if (c == '\'' && !in_double_quote && dollar_start == std::string::npos) {
475476
in_single_quote = !in_single_quote;
476477
result << c;
477478
}
@@ -594,6 +595,7 @@ std::pair<std::string, std::string> cadabra::convert_line(const std::string& lin
594595
};
595596

596597
line_stripped = replace_dollar_expressions(line_stripped, replacement);
598+
DEBUGLN( std::cerr << "line_stripped = " << line_stripped << std::endl; );
597599

598600
// std::regex dollarmatch(R"(\$([^\$]*)\$)");
599601
// line_stripped = std::regex_replace(line_stripped, dollarmatch, "Ex(r'''$1''', False)", std::regex_constants::match_default | std::regex_constants::format_default );

core/properties/Indices.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ const std::vector<Ex>& Indices::values(const Properties& properties, Ex::iterato
180180
// throw ArgumentException("No explicit values set for indices, and no Integer property found.");
181181

182182
if(!iv->from.is_integer() || !iv->to.is_integer())
183-
throw ArgumentException("Indicex has integer property, but explicit range needed.");
183+
throw ArgumentException("Indices have integer property, but explicit range needed.");
184184

185185
for(int val = iv->from.to_integer(); val <= iv->to.to_integer(); ++val)
186186
values_.push_back(Ex(val));

examples/sample_dyn.cnb

Lines changed: 151 additions & 0 deletions
Large diffs are not rendered by default.

tests/substitute.cdb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,3 +662,15 @@ def test132():
662662
print("Test 132 passed")
663663

664664
test132()
665+
666+
def test133():
667+
__cdbkernel__=create_scope()
668+
ex:= A + B3 + C7.
669+
substitute(_, $A + M? + N? | \regex{M?}{"[A-Z]7"} -> \sin(M? N?)/N?$)
670+
tst:= \sin(C7 B3)/B3 - @(ex);
671+
assert(tst==0)
672+
print("Test 133 passed")
673+
674+
test133()
675+
676+

web2/cadabra2/source/changelog.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ <h3>github devel branch (2.5.13)</h3>
3939
<li>AppImages are back, on both x86_64 and arm64.</li>
4040
<li>Fix loading <tt>cadabra2_defaults.py</tt> in the Jupyter kernel.</li>
4141
<li>Fix scrolling bug in notebook which would make the document jump around for no good reason.</li>
42+
<li>Fix homebrew package (by avoiding svg icons on macOS, as homebrew does not package librsvg correctly).</li>
43+
<li>Internal: refactor to enable cleaner insertion of related properties on property declaration.</li>
44+
<li>Fix handling substitute with regex (<a href="https://github.com/kpeeters/cadabra2/issues/220">#220</a>, David).</li>
4245
</ul>
4346

4447
<a name="2.5.12"></a>

0 commit comments

Comments
 (0)