From ed300116bd55e66e9a28b49d2263a02b8b55c785 Mon Sep 17 00:00:00 2001 From: Dirk Zimoch Date: Tue, 21 May 2019 09:24:54 +0200 Subject: [PATCH] add tolower and toupper to regsubst --- docs/formats.html | 12 +++++++++++- src/RegexpConverter.cc | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/docs/formats.html b/docs/formats.html index c08b923..cac1788 100644 --- a/docs/formats.html +++ b/docs/formats.html @@ -601,7 +601,7 @@ It is only available if a PCRE library is installed.

If PCRE is not available for your host or cross architecture, download -the sourcecode from www.pcre.org +the sourcecode from www.pcre.org and try my EPICS compatible Makefile to compile it like a normal EPICS support module. @@ -656,6 +656,13 @@ Matches of the regex are replaced by the string subst with all \1 through \9 replaced with the match of the corresponding sub-expression if such a sub-expression exists. +Occurrences of \Un, \Ln, \un, +or \ln with n being a number 0 +through 9 or & are replaced with the corresponding sub-expression +converted to all upper case, all lower case, first letter upper case, or first letter lower +case, respectively. +

+

Due to limitations of the parser, \1 and \x01 are the same which makes it difficult to use literal bytes with values lower than 10 in subst. Therefore \0 aways means a literal byte (incompatible change from earlier version!) @@ -722,6 +729,9 @@ which is not at the end of a word. %#/([^+-])*([+-])/\2\1/ moves a postfix sign to the front. (1.23- becomes -1.23)

+
+%#-2/.*/\U0/ converts the previous 2 characters to upper case. +

15. MantissaExponent DOUBLE converter (%m)

diff --git a/src/RegexpConverter.cc b/src/RegexpConverter.cc index d6f5bf9..d3e9f2d 100644 --- a/src/RegexpConverter.cc +++ b/src/RegexpConverter.cc @@ -20,9 +20,10 @@ #include "StreamFormatConverter.h" #include "StreamError.h" -#include "string.h" #include "pcre.h" +#include #include +#include #define Z PRINTF_SIZE_T_PREFIX @@ -219,9 +220,40 @@ static void regsubst(const StreamFormat& fmt, StreamBuffer& buffer, size_t start if (s[r] == esc) { unsigned char ch = s[r+1]; - debug("found escaped \\%u, in range 1-%d?\n", ch, rc-1); - if (ch != 0 && ch < rc) // escaped 1 - 9 : replace with subexpr + if (strchr("ulUL", ch)) { + unsigned char br = s[r+2] - '0'; + if (br == (unsigned char)('&'-'0')) br = 0; + debug("found case conversion \\%c%u\n", ch, br); + if (br >= rc) + { + s.remove(r, 1); + continue; + } + br *= 2; + rl = ovector[br+1] - ovector[br]; + s.replace(r, 3, buffer(start+c+ovector[br]), rl); + switch (ch) + { + case 'u': + if (islower(s[r])) s[r] = toupper(s[r]); + break; + case 'l': + if (isupper(s[r])) s[r] = tolower(s[r]); + break; + case 'U': + for (int i = 0; i < rl; i++) + if (islower(s[r+i])) s[r+i] = toupper(s[r+i]); + break; + case 'L': + for (int i = 0; i < rl; i++) + if (isupper(s[r+i])) s[r+i] = tolower(s[r+i]); + break; + } + } + else if (ch != 0 && ch < rc) // escaped 1 - 9 : replace with subexpr + { + debug("found escaped \\%u\n", ch); ch *= 2; rl = ovector[ch+1] - ovector[ch]; debug("yes, replace \\%d: \"%s\"\n", ch/2, buffer.expand(start+c+ovector[ch], rl)()); @@ -230,7 +262,7 @@ static void regsubst(const StreamFormat& fmt, StreamBuffer& buffer, size_t start } else { - debug("no, use literal \\%u\n", ch); + debug("use literal \\%u\n", ch); s.remove(r, 1); // just remove escape } }