Using m4 to convert a string to ASCII codepoints

百般思念 提交于 2019-12-06 09:52:00

For the benefit of others interested in a pure m4 implementation I've managed to create the following conversion macro. It has to meddle with the quoting characters to support normal m4 quote chars. It could be simplified a bit if that isn't important.

changequote(<!,!>) # Change quotes so we can handle "`" and "'"

# Change quotes every time this macro is called
define(<!asciiord!>,<!changequote(<!,!>)<!!>_asciiord(<!$1!>)<!!>changequote`'dnl
!>)

# Convert chars one at a time in the string argument
define(<!_asciiord!>,<!ifelse(<!$1!>,,, dnl
  <!_aconv(substr(<!$1!>,0,1))<!!>ifelse(len(<!$1!>),1,,<!,!>) $0(substr(<!$1!>,1))!>)!>)

# Map ASCII chars to their integer index by position
# Control chars are not supported.
# If the comment character is changed you must alter the map accordingly
define(<!_aconv!>,<!ifelse(<!$1!>,<! !>,32,<!$1!>,<!#!>,35,dnl
  <!index(<!                                 !" $%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !>,<!$1!>)!>)!>)

changequote # Restore normal quoting chars

Usage:

asciiord(`hello') --> 104, 101, 108, 108, 111

I accomplished this with the following code section

divert(-1)
changequote(-{,}-)
changecom(/*,*/)
define(-{ascii}-,-{#define TKN_-{}-$1 esyscmd(-{python -c "import sys; sys.stdout.write('%d'%ord('$2'))"}-)}-)
divert(0)dnl

I would greatly appreciate if anyone knows of a better way to do this (e.g. native m4, or more portable shell command).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!