groff-commit
[Top][All Lists]
Advanced

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

[groff] 14/14: Speculative additional request .stringhex.


From: Deri James
Subject: [groff] 14/14: Speculative additional request .stringhex.
Date: Tue, 4 Jul 2023 11:22:38 -0400 (EDT)

deri pushed a commit to branch deri-gropdf-ng
in repository groff.

commit a2b2d5526dca59e93d46019fa8625e6f4c5484d5
Author: Deri James <deri@chuzzlewit.myzen.co.uk>
AuthorDate: Mon Jul 3 16:37:17 2023 +0100

    Speculative additional request .stringhex.
    
    * src/roff/troff/input.cpp:
    
    Based on .stringup/down converts the contents of named string register
    to a hex string, two hex characters for each byte. In some situations
    you want to protect a string from being interpreted for its special
    characters (i.e. unicode), this achieves that.
---
 src/roff/troff/input.cpp | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 292ee7389..0c90a7451 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -4752,8 +4752,46 @@ void chop_macro()
 }
 
 enum case_xform_mode { STRING_UPCASE, STRING_DOWNCASE };
+const char * hex = "0123456789ABCDEF";
 
 // Case-transform each byte of the string argument's contents.
+void stringhex_request()
+{
+  symbol s = get_name(true /* required */);
+  if (s.is_null()) {
+    skip_line();
+    return;
+  }
+  request_or_macro *p = lookup_request(s);
+  macro *m = p->to_macro();
+  if (!m) {
+    error("cannot apply stringhex to a request ('%1')",
+         s.contents());
+    skip_line();
+    return;
+  }
+  string_iterator iter1(*m);
+  macro *mac = new macro;
+  int len = m->macro::length();
+  for (int l = 0; l < len; l++) {
+    int nc, c = iter1.get(0);
+    if (c == PUSH_GROFF_MODE
+       || c == PUSH_COMP_MODE
+       || c == POP_GROFFCOMP_MODE)
+      mac->append(c);
+    else if (c == EOF)
+      break;
+    else {
+      nc = (int)hex[(c >> 4) & 0x0f];
+      mac->append(nc);
+      nc = (int)hex[c & 0x0f];
+      mac->append(nc);
+    }
+  }
+  request_dictionary.define(s, mac);
+  tok.next();
+}
+
 void do_string_case_transform(case_xform_mode mode)
 {
   assert((mode == STRING_DOWNCASE) || (mode == STRING_UPCASE));
@@ -8435,6 +8473,7 @@ void init_input_requests()
   init_request("soquiet", source_quietly);
   init_request("spreadwarn", spreadwarn_request);
   init_request("stringdown", stringdown_request);
+  init_request("stringhex", stringhex_request);
   init_request("stringup", stringup_request);
   init_request("substring", substring_request);
   init_request("sy", system_request);



reply via email to

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