Browse Source

[refactor] WrappedBuffer
[quickfix][minor] mem mamagment

isundil 9 years ago
parent
commit
8d4eab7c18
7 changed files with 39 additions and 21 deletions
  1. 2 2
      include/streamConsumer.hh
  2. 21 8
      include/wrappedBuffer.hpp
  3. 1 1
      src/curseOutput.cpp
  4. 3 4
      src/main.cpp
  5. 6 5
      src/streamConsumer.cpp
  6. 2 1
      test/src/main.cpp
  7. 4 0
      test/testUnicode.json

+ 2 - 2
include/streamConsumer.hh

@@ -11,15 +11,15 @@
 class StreamConsumer
 {
     public:
+        StreamConsumer(std::istream &stream);
         virtual ~StreamConsumer();
 
-        static StreamConsumer *read(std::istream &stream, const AParams *params=nullptr);
+        StreamConsumer *read();
         JSonElement * const getRoot() const;
 
         StreamConsumer *withConfig(const AParams *);
 
     private:
-        StreamConsumer(std::istream &stream);
         /**
          * @return true on success
         **/

+ 21 - 8
include/wrappedBuffer.hpp

@@ -115,18 +115,31 @@ unsigned int WrappedBuffer<T, SIZE>::size() const
     return (curR > curW) ? (SIZE - curR + curW +1) : (curW - curR +1);
 }
 
+#include <iostream>
+
 template<typename T, int SIZE>
 std::basic_string<T> WrappedBuffer<T, SIZE>::toString() const
 {
-    std::basic_string<T> result(size(), (T) 0);
     const unsigned int size = this->size();
-    int from = (curR == SIZE) ? 0 : curR;
-    unsigned int j = 0;
-
-    for (int i = from; (curW > curR && i <= curW) || (curW <= curR && i < SIZE); ++i)
-        result[j++] = buffer[i];
-    for (int i = 0; j < size; ++i)
-        result[j++] = buffer[i];
+    std::basic_string<T> result;
+    if (!size)
+        return result;
+    result.reserve(size);
+    const int from = (curR == SIZE) ? 0 : curR;
+    int i = 0;
+    unsigned int j =0;
+
+    for (i = from; (curW >= from && i <= curW) || (curW < from && i < SIZE); ++i)
+    {
+        j++;
+        result += buffer[i];
+    }
+    i = 0;
+    while (i <= curW && j < size)
+    {
+        result += buffer[i++];
+        j++;
+    }
     return result;
 }
 

+ 1 - 1
src/curseOutput.cpp

@@ -33,7 +33,7 @@ void CurseOutput::loop()
     breakLoop = false;
     do
     {
-        while (!redraw())
+        while (!redraw()) //TODO opti going down
             ;
     } while(readInput());
 }

+ 3 - 4
src/main.cpp

@@ -7,14 +7,14 @@
 
 void run(Params *params)
 {
-    StreamConsumer *stream;
+    StreamConsumer stream(StreamConsumer(params->getInput()));
+    stream.withConfig(params);
     CurseOutput *out;
     JSonElement *root;
 
     try
     {
-        stream = StreamConsumer::read(params->getInput(), params);
-        root = stream->getRoot();
+        root = stream.read()->getRoot();
         if (!root)
             throw EofException();
     }
@@ -33,7 +33,6 @@ void run(Params *params)
     out = new CurseOutput(root, *params);
     out->run();
     delete out;
-    delete stream;
 }
 
 int main(int ac, char **av)

+ 6 - 5
src/streamConsumer.cpp

@@ -4,7 +4,7 @@
 #include "jsonElement.hh"
 #include "streamConsumer.hh"
 
-StreamConsumer::StreamConsumer(std::istream &s): stream(s)
+StreamConsumer::StreamConsumer(std::istream &s): stream(s), root(nullptr)
 { }
 
 StreamConsumer::~StreamConsumer()
@@ -19,11 +19,12 @@ StreamConsumer *StreamConsumer::withConfig(const AParams *p)
     return this;
 }
 
-StreamConsumer *StreamConsumer::read(std::istream &stream, const AParams *config)
+StreamConsumer *StreamConsumer::read()
 {
-    StreamConsumer *inst = (new StreamConsumer(stream))->withConfig(config);
-    inst->root = inst->readNext(nullptr);
-    return inst;
+    if (root)
+        return this;
+    root = readNext(nullptr);
+    return this;
 }
 
 JSonElement *StreamConsumer::readNext(JSonContainer *parent)

+ 2 - 1
test/src/main.cpp

@@ -11,7 +11,7 @@ const char testJson[] = "{\"widget\": {\"debug\": \"on\",\"window\": {\"title\":
 StreamConsumer *toJson(std::string str)
 {
     std::stringstream input(str);
-    return StreamConsumer::read(input);
+    return (new StreamConsumer(input))->read();
 }
 
 void checkArray()
@@ -113,6 +113,7 @@ void checkSample()
 {
     StreamConsumer *root = toJson(testJson);
     root->getRoot();
+    delete root;
 }
 
 int main(int ac, char **av)

+ 4 - 0
test/testUnicode.json

@@ -0,0 +1,4 @@
+[
+    "ascii-only",
+    "\u058e",
+]