How do I define custom literals?

I am converting some source code from One Scripting Language (PAWN) to Programming Language (C ++) on Windows.

The source code contains millions of binary literals all over the place in the form:

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

      

To my regret, Visual Studio 2013 does not support the standard user standard.

Is there any wya to achieve this? 010101_b or something with C ++, maybe with a little boost added?

+3


source to share


2 answers


I highly recommend that you convert your source code using a script.

Anyway, if you are interested in 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 can slow down compile times a lot. So, try converting your original code instead.

+1


source


I suggest writing a small console program that converts binary literals to hex literals.

In this process, I would use:

  • Copy the file.
  • Using a text editor, remove everything before the first literal.
  • Remove everything after the last literal.
  • Save.
  • Replace "," with a new line.
  • Save.


You now have a text file with binary literals, one per line.

Write a program to read to a file, convert to hex literals and output.

Then edit this file to insert all the missing things.

0


source







All Articles