Browse Source

[Fix #18] search for \uXXXX will match corresponding unicode wchars

isundil 9 years ago
parent
commit
2401d32c29
6 changed files with 63 additions and 23 deletions
  1. 16 1
      include/curseOutput.hh
  2. 1 3
      include/searchPattern.hh
  3. 8 1
      include/streamConsumer.hh
  4. 12 1
      src/curseOutput.cpp
  5. 4 17
      src/searchPattern.cpp
  6. 22 0
      src/streamConsumer.cpp

+ 16 - 1
include/curseOutput.hh

@@ -18,7 +18,6 @@ class JSonElement;
 class JSonContainer;
 class JSonArray;
 class JSonObject;
-template<class T> class Optional;
 class SearchPattern;
 
 class CurseOutput
@@ -48,10 +47,12 @@ class CurseOutput
          * Initialize ncurses
         **/
         void init();
+
         /**
          * Release ncurses
         **/
         void shutdown();
+
         /**
          * return false if bottom of screen is touched
          * redraw all data
@@ -65,19 +66,23 @@ class CurseOutput
          * redraw item and children
         **/
         bool redraw(std::pair<int, int> &, const std::pair<unsigned int, unsigned int> &maxWidth, const JSonElement *item);
+
         /**
          * Wait for input
          * @return false if ncurses should stop
         **/
         bool readInput();
+
         /**
          * get the screen size
         **/
         const std::pair<unsigned int, unsigned int> getScreenSize() const;
+
         /**
          * set the select_up and select_down pointers, scroll to selection if it is above view port
         **/
         void checkSelection(const JSonElement *item, const std::pair<int, int>&);
+
         /**
          * Return the number of lines written while writting nbChar bytes
          * @param nbChar col written
@@ -85,6 +90,7 @@ class CurseOutput
          * @return the number of line written
         **/
         static unsigned int getNbLines(float nbChar, unsigned int maxWidth);
+
         /**
          * get flags to be passed to write.
          * Contains indications on who to write item
@@ -118,6 +124,7 @@ class CurseOutput
          * @throws noInputException if user use a kill-key (to exit buffer)
         **/
         const SearchPattern *inputSearch();
+
         /**
          * find occurences of search result and fill this#search_result
         **/
@@ -145,10 +152,12 @@ class CurseOutput
          * Root item
         **/
         const JSonElement *data;
+
         /**
          * selected item
         **/
         const JSonElement *selection;
+
         /**
          * currently searching pattern and its results
         **/
@@ -158,27 +167,33 @@ class CurseOutput
          * prev/next items to be selected on up/down keys
         **/
         const JSonElement *select_up, *select_down;
+
         /**
          * Program params (ac/av)
         **/
         const Params &params;
+
         /**
          * ncurses stuff
         **/
         SCREEN *screen;
         FILE *screen_fd;
+
         /**
          * Used by signals to stop reading input and shutdown ncurses
         **/
         bool breakLoop;
+
         /**
          * Viewport start
         **/
         int scrollTop;
+
         /**
          * initialized colors
         **/
         std::set<char /* OutputFlag::TYPE_SOMETHING */> colors;
+
         /**
          * Selection helpers
          * Used for moving viewport

+ 1 - 3
include/searchPattern.hh

@@ -7,8 +7,7 @@ class JSonElement;
 class SearchPattern
 {
     public:
-        SearchPattern(const char *);
-        SearchPattern(const std::wstring &);
+        SearchPattern(const std::string &);
         ~SearchPattern();
 
         bool isEmpty() const;
@@ -20,7 +19,6 @@ class SearchPattern
         bool operator()(char a, char b);
 
     private:
-        void init(const char *);
         void evalFlags(const char *);
 
         std::string pattern;

+ 8 - 1
include/streamConsumer.hh

@@ -45,6 +45,12 @@ class StreamConsumer
         **/
         StreamConsumer *withConfig(const AParams *);
 
+        /**
+         * find \uXXXX in buffer and replace them
+        **/
+        static std::string extractUnicode(const char *);
+        static std::string extractUnicode(const std::string &);
+
     private:
         /**
          * @return non-null on successfully read JSonElement, or null if token (',', '[', ...)
@@ -59,7 +65,6 @@ class StreamConsumer
          * read next item, fill object or array if found
         **/
         JSonElement *readNext(JSonContainer *parent);
-
         /**
          * fill object
         **/
@@ -68,10 +73,12 @@ class StreamConsumer
          * fill array
         **/
         JSonArray *readArray(JSonContainer *parent);
+
         /**
          * out of token, should we ignore that char ?
         **/
         bool ignoreChar(char c) const noexcept;
+
         /**
          * compute unicode value and append it to buffer
         **/

+ 12 - 1
src/curseOutput.cpp

@@ -18,6 +18,7 @@
 #include "jsonObject.hh"
 #include "jsonArray.hh"
 #include "except.hh"
+#include "streamConsumer.hh"
 
 static CurseOutput *runningInst = nullptr;
 class SelectionOutOfRange { };
@@ -643,7 +644,17 @@ const SearchPattern *CurseOutput::inputSearch()
     wtimeout(stdscr, 150);
     curs_set(false);
 
-    return abort ? nullptr : new SearchPattern(buffer);
+    {
+        const size_t size = buffer.size();
+        char bytesString[size * sizeof(wchar_t)];
+        wcstombs(&bytesString[0], buffer.c_str(), sizeof(bytesString));
+        std::string str;
+        if (params.isIgnoringUnicode())
+            str = bytesString;
+        else
+            str = StreamConsumer::extractUnicode(bytesString);
+        return abort ? nullptr : new SearchPattern(str);
+    }
 }
 
 void CurseOutput::writeBottomLine(const std::string &buffer, short color) const

+ 4 - 17
src/searchPattern.cpp

@@ -5,23 +5,7 @@
 #include "jsonObjectEntry.hh"
 #include "jsonPrimitive.hh"
 
-SearchPattern::SearchPattern(const char *input): flags(0)
-{
-    init(input);
-}
-
-SearchPattern::SearchPattern(const std::wstring &buffer)
-{
-    const size_t size = buffer.size();
-    char bytesString[size * sizeof(wchar_t)];
-    wcstombs(&bytesString[0], buffer.c_str(), sizeof(bytesString));
-    init(bytesString);
-}
-
-SearchPattern::~SearchPattern()
-{ }
-
-void SearchPattern::init(const char *input)
+SearchPattern::SearchPattern(const std::string &input): flags(0)
 {
     size_t pos = 0;
     bool escaped = false;
@@ -52,6 +36,9 @@ void SearchPattern::init(const char *input)
     pattern = ss.str();
 }
 
+SearchPattern::~SearchPattern()
+{ }
+
 void SearchPattern::evalFlags(const char *s)
 {
     while (*s)

+ 22 - 0
src/streamConsumer.cpp

@@ -364,6 +364,28 @@ void StreamConsumer::appendUnicode(const char unicode[4], std::stringstream &buf
     buf.write(test, 4);
 }
 
+std::string StreamConsumer::extractUnicode(const char *buf)
+{
+    std::stringstream result;
+
+    for (; *buf; buf++)
+    {
+        if (*buf == '\\' && buf[1] == 'u' && buf[2] && buf[3] && buf[4] && buf[5])
+        {
+            appendUnicode(buf +2, result);
+            buf += 6;
+        }
+        else
+            result.write(buf, 1);
+    }
+    return result.str();
+}
+
+std::string StreamConsumer::extractUnicode(const std::string &buf)
+{
+    return extractUnicode(buf.c_str());
+}
+
 bool StreamConsumer::ignoreChar(char c) const noexcept
 {
     return (c <= 32 || c >= 127 || c == '\n');