我受到了this answer的启发。
我想到的一种方式是将static_assert (C11-更高版本)和assert结合起来。
基本上,一个ascii.h文件包含一堆
static_assert(' ' == 32);
static_assert('!' == 33);
// ...语句,然后是函数
static void assert_runtime_ascii(void) {
assert(' ' == 32);
assert('!' == 33);
// ...
}将在main中执行。
这太冗长了(200+行),这使得它不切实际,所以我只对此进行注释
// NOTE: this program expects ASCII both at run-time and compile-time.就完事了。
有没有更好的方法?
我在标准中找不到像__STDC_IEC_559__这样的ASCII码的#define。
有没有更好的方法?
发布于 2020-02-08 09:45:29
是在编译时和运行时都保证ASCII码的最佳方式吗?
听起来,OP想知道如何检测执行字符集编码和用户I/O编码。
在编译时或运行时,要检测ASCII,请对128个ASCII字符中的大多数使用一系列ASCII或asserts (example code),这些字符是C语言执行字符集的一部分。C11§5.2.1 3
A to Z
a to z
0 to 9
! " # % & ’ ( ) * + , - . / : ; < = > ? [ \ ] ^ _ { | } ~
space character,
and control characters representing horizontal tab, vertical tab, and form feed.
some way of indicating the end of each line of text请注意,像"$,@,`“这样的字符不是执行字符集的一部分,因此”如果没有相应的成员,它将被转换为除空(宽)字符以外的实现定义的成员“。
要检测用户输入/输出的编码,除了要求用户输入引用文本并对其进行代码检查之外,我找不到任何解决方案。
https://stackoverflow.com/questions/60122693
复制相似问题