title: copy first n chars to an std::string
body: <p>I have a char pointer that points to a buffer of unknown length:</p>
  
  <pre><code>char* buffer;
  </code></pre>
  
  <p>I need to copy the first N (N is an input variable) chars to an std::string if N !=0.
  My current code is:</p>
  
  <pre><code>int N = &lt;something&gt;;
  std::string text;
  if (N != 0)
      text.assign(buffer, N);
  else
      text.assign(buffer);
  </code></pre>
  
  <p>the thing I do not like, is that when the size of buffer is less than N, the resulting size of text is N.</p>
  
  <p>Is there any function in the standard library to copy the first N chars of buffer to text without forcing the size of text to N?</p>
  
  <p>i.e if buffer is only 1 char and N = 100, I want that text.size() = 1</p>
  
  <p>Is a custom copy, byte by byte, the solution?</p>