title: boost::asio performant serial read (RS232)
body: <p>I have the following method (located in a class) taken from: <a href="https://gitorious.org/serial-port/serial-port/source/03e161e0b788d593773b33006e01333946aa7e13:1_simple/SimpleSerial.h#L46" rel="nofollow">https://gitorious.org/serial-port/serial-port/source/03e161e0b788d593773b33006e01333946aa7e13:1_simple/SimpleSerial.h#L46</a> </p>
  
  <pre><code>        boost::asio::serial_port serial;
          // [...]
  
          std::string readLine() {
          char c;
          std::string result;
          while(true) {
  
              asio::read(serial,asio::buffer(&amp;c,1));
              switch(c)
              {
                  case '\r':
                      result+=c;
                      break;
                  case '\n':
                      result+=c;
                      return result;
                  default:
                      result+=c;
              }
          }
  
          return result;
        }
  </code></pre>
  
  <p>As it is written there "code is optimized for simplicity, not speed". So I am thinking about optimizing this code. However I dont get any useful results. My general approach was this:</p>
  
  <pre><code>    void readUntil(const std::string&amp; delim) {
          using namespace boost;
  
          asio::streambuf bf;
          size_t recBytes = asio::read_until(serial, bf, boost::regex(delim));
          [...]
  </code></pre>
  
  <p>'delim' would be "\n". But I do not know how to convert asio::streambuf to a std::string. Furthermore I do not know if I lose characters with this approach. E.g. if I receive the following chunk of text at once:</p>
  
  <pre><code> xxxxx\r\nyyyyyyy
  </code></pre>
  
  <p>Would I just read 'xxxxx\r\n' and the rest is lost?</p>