Преглед на файлове

fix https://github.com/blueloveTH/pocketpy/issues/179

blueloveTH преди 2 години
родител
ревизия
9e66bab34b
променени са 2 файла, в които са добавени 15 реда и са изтрити 2 реда
  1. 1 0
      include/pocketpy/str.h
  2. 14 2
      src/str.cpp

+ 1 - 0
include/pocketpy/str.h

@@ -26,6 +26,7 @@ struct Str{
     Str(std::nullptr_t) { FATAL_ERROR(); }
     Str(const char* s);
     Str(const char* s, int len);
+    Str(std::pair<char *, int>);
     Str(const Str& other);
     Str(Str&& other);
 

+ 14 - 2
src/str.cpp

@@ -42,6 +42,19 @@ int utf8len(unsigned char c, bool suppress){
 
 #undef STR_INIT
 
+    Str::Str(std::pair<char *, int> detached) {
+        this->size = detached.second;
+        this->data = detached.first;
+        this->is_ascii = true;
+        // check is_ascii
+        for(int i=0; i<size; i++){
+            if(!isascii(data[i])){
+                is_ascii = false;
+                break;
+            }
+        }
+    }
+
     Str::Str(const Str& other): size(other.size), is_ascii(other.is_ascii) {
         _alloc();
         memcpy(data, other.data, size);
@@ -423,8 +436,7 @@ int utf8len(unsigned char c, bool suppress){
 
     Str SStream::str(){
         // after this call, the buffer is no longer valid
-        auto detached = buffer.detach();
-        return Str(detached.first, detached.second);
+        return Str(buffer.detach());
     }
 
     SStream& SStream::operator<<(const Str& s){