简体   繁体   English

如何定义自定义文字?

[英]How can I define custom literals?

I am converting some source code from one scripting language (PAWN) to a programming language (C++) on Windows. 我正在将一些源代码从一种脚本语言(PAWN)转换为Windows上的编程语言(C ++)。

The source code has millions of binary literals all over the place in the form of: 源代码具有数百万个二进制文字,其形式为:

data[] = 
{
    0b11111111111011111110110111111110, 0b00000000001111111111111111111111,
    0b00000000000000000000000000000000, 0b00000000000000000000000000000000,
    0b00000000000000000000000000000000, 0b00000000000000000000000000000000,
    0b00000000000000000000000000000000, 0b00000000000000000000000000000000,
    ///some million lines later...
    0b00000000000000000000000000000000, 0b11111111111111111111111110000000,
    0b11100001001111111111111111111111, 0b11110111111111111111111111111111,
    0b11111111111111111111111111111111, 0b11111111111111111111111111111111,

To my unfortunate luck Visual Studio 2013 doesn't support the user defined literals standard. 令我不幸的是,Visual Studio 2013不支持用户定义的文字标准。

Is there any wya to achieve this somehow? 是否有任何wya以某种方式实现这一目标? 010101_b or something with C++, maybe with a little addition of boost? 010101_b或者带有C ++的东西,可能还有一点点提升?

I strongly advise you to transform the source code with a script. 我强烈建议您使用脚本转换源代码。

Anyway, if you're interested in Boost.PP: 无论如何,如果你对Boost.PP感兴趣:

#define FROM_BINARY(s, data, elem) sum(#elem+2)

constexpr auto sum(char const* str, std::uintmax_t val = 0) -> decltype(val)
{
    return !*str? val : sum(str+1, (val << 1) + *str - '0');
}

unsigned data[]
{
    BOOST_PP_TUPLE_REM_CTOR(BOOST_PP_SEQ_TO_TUPLE(
        BOOST_PP_SEQ_TRANSFORM(FROM_BINARY, , 
            BOOST_PP_VARIADIC_TO_SEQ(
    0b11111111111011111110110111111110, 0b00000000001111111111111111111111,
    0b00000000000000000000000000000000, 0b00000000000000000000000000000000,
    0b00000000000000000000000000000000, 0b00000000000000000000000000000000,
    0b00000000000000000000000000000000, 0b00000000000000000000000000000000,
    ///some million lines later...
    0b00000000000000000000000000000000, 0b11111111111111111111111110000000,
    0b11100001001111111111111111111111, 0b11110111111111111111111111111111,
    0b11111111111111111111111111111111, 0b11111111111111111111111111111111))))
};

Note that this could horribly slow down compile time. 请注意,这可能会大大减慢编译时间。 So, again, try to transform the source dode once instead. 因此,再次尝试转换源dode一次。

I suggest writing a simple, small, console program that converts the binary literals into hexadecimal literals. 我建议编写一个简单的小型控制台程序,将二进制文字转换为十六进制文字。

Here's the process I would use: 这是我将使用的过程:

  1. Copy the file. 复制文件。
  2. Using a text editor, remove everything before the first literal. 使用文本编辑器删除第一个文字之前的所有内容。
  3. Remove everything after the last literal. 删除最后一个文字后的所有内容。
  4. Save. 救。
  5. Replace the ", " with a newline. 用换行符替换“,”。
  6. Save. 救。

You now have a text file with the binary literals, one per line. 您现在有一个带有二进制文字的文本文件,每行一个。

Write a program to read in the file, convert to hexadecimal literals and output. 编写一个程序来读入文件,转换为十六进制文字和输出。

Then edit this file by pasting in all the missing stuff. 然后通过粘贴所有缺失的东西来编辑这个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM