From 2d5650767ad94d1a9c581ce39bcb158dee9b6f9f Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Wed, 1 Jan 2020 17:04:19 -0500 Subject: [PATCH] Reconciled parsing of field names with Substitution Field Spec. (#82) According to the spec., valid syntax for merge field names is determined by the merge source with a couple of minor exceptions. Current text backends allow any printable characters, so the SubstitutionField parser now accepts any printable characters. --- model/SubstitutionField.cpp | 2 +- model/unit_tests/TestSubstitutionField.cpp | 24 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/model/SubstitutionField.cpp b/model/SubstitutionField.cpp index 76c4047..0798bef 100644 --- a/model/SubstitutionField.cpp +++ b/model/SubstitutionField.cpp @@ -145,7 +145,7 @@ namespace glabels { bool success = false; - while ( s.size() && (s[0].isDigit() || s[0].isLetter() || s[0] == '_' || s[0] == '-') ) + while ( s.size() && (s[0].isPrint() && s[0] != ':' && s[0] != '}') ) { field.mFieldName.append( s[0] ); s = s.mid(1); diff --git a/model/unit_tests/TestSubstitutionField.cpp b/model/unit_tests/TestSubstitutionField.cpp index 04476b6..16995f7 100644 --- a/model/unit_tests/TestSubstitutionField.cpp +++ b/model/unit_tests/TestSubstitutionField.cpp @@ -33,7 +33,7 @@ void TestSubstitutionField::parseValid() // // Valid substitution fields (concatenated in single input string) // - QString input = "${1234}${abc:=ABC}${x:%08.2f}${y:%08.2f:=12.34}${ADDR2:n}"; + QString input = "${1234}${abc:=ABC}${x:%08.2f}${y:%08.2f:=12.34}${ADDR2:n}${FüññýßútLæg@lѪmê}${Also_a legal-name}"; QStringRef s = &input; model::SubstitutionField f1; @@ -66,6 +66,16 @@ void TestSubstitutionField::parseValid() QCOMPARE( model::SubstitutionField::parse( s, f5 ), true ); QCOMPARE( f5.fieldName(), QString( "ADDR2" ) ); QCOMPARE( f5.newLine(), true ); + + model::SubstitutionField f6; + QCOMPARE( model::SubstitutionField::parse( s, f6 ), true ); + QCOMPARE( f6.fieldName(), QString( "FüññýßútLæg@lѪmê" ) ); + QCOMPARE( f6.newLine(), false ); + + model::SubstitutionField f7; + QCOMPARE( model::SubstitutionField::parse( s, f7 ), true ); + QCOMPARE( f7.fieldName(), QString( "Also_a legal-name" ) ); + QCOMPARE( f7.newLine(), false ); } @@ -108,6 +118,18 @@ void TestSubstitutionField::parseInvalid() QStringRef s9 = &input9; model::SubstitutionField f9; QCOMPARE( model::SubstitutionField::parse( s9, f9 ), true ); + + QString input10 = "${embedded\nnew-line}"; + QStringRef s10 = &input10; + model::SubstitutionField f10; + QCOMPARE( model::SubstitutionField::parse( s10, f10 ), false ); + QCOMPARE( s10, QStringRef( &input10 ) ); // Should not advance string reference + + QString input11 = "${how-about-a\ttab}"; + QStringRef s11 = &input11; + model::SubstitutionField f11; + QCOMPARE( model::SubstitutionField::parse( s11, f11 ), false ); + QCOMPARE( s11, QStringRef( &input11 ) ); // Should not advance string reference }