6/25/2023 0 Comments Caesar cipherI don't like using a macro to concatenate two copies of the alphabet.Most of the ugliness is in create_table(), which in an earlier version had been an immediately-invoked lambda expression in the constructor's initialiser list I moved it out to a named private function to make it easier to read and easier to skip when perusing the public interface.I don't have access to an EBCDIC system to confirm my claim that it's correct on such platforms.I may write a separate wide-char implementation that addresses the limitations.But it's still small compared with the overhead of creating a process, and it's a big win for large inputs. There is an overhead to constructing the table each run, which won't pay for itself with short input streams.No problem for systems where CHAR_BIT is 8, but it may prevent use on systems with much wider character type. Because we use a table to produce output, we need storage for that table, sufficient to represent all character values.When using codings that shift out the alpha-chars, then non-alphabetic characters may be modified - but using the same program for the decoding will correctly restore the original input text. It works with multi-byte character sets where the alphabetic characters are invariant (e.g.Unlike many implementations, it works on systems where alphabetic characters are discontinuous, such as EBCDIC. It works with all single-byte character sets.We can shift by any representable integer (negative shift decodes a positive shift, and vice versa).(But don't let that inhibit reviews from suggesting improvements that require newer versions - I am still interested!) It's written using portable C++ which compiles with any compiler supporting C++11 or later.Std::iota(table.begin(), table.end(), 0) įor (auto i = 0 i (std::toupper(alpha2)) normalise to the smallest positive equivalent Static const auto* alpha2 = reinterpret_cast(LETTERS LETTERS) Static char_table create_table(int rotation)Ĭonstexpr int len = (sizeof LETTERS) - 1 // don't count the terminating null #define LETTERS "abcdefghijklmnopqrstuvwxyz" My version is intended to be efficient and portable (subject to some limitations, below). ![]() Implementation of a Caesar cipher is a popular exercise and there are many implementations posted here on Code Review.
0 Comments
Leave a Reply. |