Skip to content
Snippets Groups Projects
Commit a307da34 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

aql: more code to cpp

parent 79493399
No related branches found
No related tags found
1 merge request!203Adress some more clang-tidy issues
...@@ -3,25 +3,27 @@ ...@@ -3,25 +3,27 @@
#include <readline/history.h> #include <readline/history.h>
   
#include <cstring> #include <cstring>
#include <array>
   
char ReadlinePromptHistory::esc_char [] = { '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\' }; static constexpr std::array < std::pair < char, char >, 8 > esc_char_list = { { { '\a', 'a'}, {'\b', 'b'}, {'\f', 'f'}, {'\n', 'n'}, {'\r', 'r'}, {'\t', 't'}, {'\v', 'v'}, {'\\', '\\'} } };
char ReadlinePromptHistory::essc_str [] = { 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\' };
size_t ReadlinePromptHistory::esc_char_size = sizeof ( ReadlinePromptHistory::esc_char );
   
char * ReadlinePromptHistory::descape ( const char * buffer ) { char * ReadlinePromptHistory::descape ( const char * buffer ) {
size_t l = strlen ( buffer ); size_t l = strlen ( buffer );
   
char * dest = static_cast < char * > ( malloc ( ( l + 1 ) * sizeof ( char ) ) ); char * dest = static_cast < char * > ( malloc ( ( l + 1 ) * sizeof ( char ) ) );
char * ptr = dest; char * ptr = dest;
for ( size_t i = 0; i < l; ++ i ) { for ( size_t i = 0; i < l; ++ i ) {
if ( buffer [ i ] == '\\' ) { if ( buffer [ i ] == '\\' ) {
++ i; ++ i;
size_t j = std::find ( essc_str, essc_str + esc_char_size, buffer [ i ] ) - essc_str; auto find_by_escaped = [ & ] ( const std::pair < char, char > & esc_pair ) { return esc_pair.second == buffer [ i ]; };
if ( j == esc_char_size ) { std::array < std::pair < char, char >, 8 >::const_iterator iter = std::find_if ( esc_char_list.begin ( ), esc_char_list.end ( ), find_by_escaped );
if ( iter == esc_char_list.end ( ) ) {
free ( dest ); free ( dest );
return strdup ( buffer ); return strdup ( buffer );
} else {
* ptr ++ = iter->first;
} }
* ptr ++ = esc_char [ j ];
} else { } else {
* ptr ++ = buffer [ i ]; * ptr ++ = buffer [ i ];
} }
...@@ -36,13 +38,15 @@ char * ReadlinePromptHistory::escape ( const char * buffer){ ...@@ -36,13 +38,15 @@ char * ReadlinePromptHistory::escape ( const char * buffer){
   
char * dest = static_cast < char * > ( malloc ( ( l * 2 + 1 ) * sizeof ( char ) ) ); char * dest = static_cast < char * > ( malloc ( ( l * 2 + 1 ) * sizeof ( char ) ) );
char * ptr = dest; char * ptr = dest;
for ( size_t i = 0; i < l; ++ i ) { for ( size_t i = 0; i < l; ++ i ) {
size_t j = std::find ( esc_char, esc_char + esc_char_size, buffer [ i ] ) - esc_char; auto find_by_actual = [ & ] ( const std::pair < char, char > & esc_pair ) { return esc_pair.first == buffer [ i ]; };
if ( j == esc_char_size ) { std::array < std::pair < char, char >, 8 >::const_iterator iter = std::find_if ( esc_char_list.begin ( ), esc_char_list.end ( ), find_by_actual );
if ( iter == esc_char_list.end ( ) ) {
* ptr ++ = buffer [ i ]; * ptr ++ = buffer [ i ];
} else { } else {
* ptr ++ = '\\'; * ptr ++ = '\\';
* ptr ++ = essc_str [ j ]; * ptr ++ = iter->second;
} }
} }
* ptr = '\0'; * ptr = '\0';
......
...@@ -3,10 +3,6 @@ ...@@ -3,10 +3,6 @@
#include <string> #include <string>
   
class ReadlinePromptHistory { class ReadlinePromptHistory {
static char esc_char [];
static char essc_str [];
static size_t esc_char_size;
static char * descape ( const char * buffer ); static char * descape ( const char * buffer );
   
static char * escape ( const char * buffer); static char * escape ( const char * buffer);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment