- Fix insert() for strings in pas2c
authorunc0rr
Thu, 11 Oct 2018 23:43:31 +0200
changeset 13880 5f819b90d479
parent 13879 6b2c87490f0a
child 13881 99b265e0d1d0
- Fix insert() for strings in pas2c - Implement insert() for ansistrings
hedgewars/uLocale.pas
project_files/hwc/rtl/pas2c.h
project_files/hwc/rtl/system.c
project_files/hwc/rtl/system.h
--- a/hedgewars/uLocale.pas	Tue Oct 09 12:54:40 2018 -0400
+++ b/hedgewars/uLocale.pas	Thu Oct 11 23:43:31 2018 +0200
@@ -197,10 +197,7 @@
         else
             begin
             delete(tempstr, p, 2);
-//FIXME rtl/system needs an ansi insert
-{$IFNDEF PAS2C}
             insert(curArg, tempstr, p);
-{$ENDIF}
             end;
     end;
 FormatA:= tempstr;
--- a/project_files/hwc/rtl/pas2c.h	Tue Oct 09 12:54:40 2018 -0400
+++ b/project_files/hwc/rtl/pas2c.h	Thu Oct 11 23:43:31 2018 +0200
@@ -24,6 +24,7 @@
     {
         struct {
             uint16_t len;
+            unsigned char str[MAX_ANSISTRING_LENGTH];
         };
         struct {
             unsigned char _dummy2;
--- a/project_files/hwc/rtl/system.c	Tue Oct 09 12:54:40 2018 -0400
+++ b/project_files/hwc/rtl/system.c	Thu Oct 11 23:43:31 2018 +0200
@@ -90,6 +90,7 @@
 
     // don't overflow on insert
     if (num_preshift > 255) {
+        num_preshift = 255;
         num_insert = 255 - (index - 1);
         num_shift = 0;
     }
@@ -114,6 +115,47 @@
     dst->len = num_shift + num_preshift;
 }
 
+void __attribute__((overloadable)) fpcrtl_insert__vars(astring *src, astring *dst, SizeInt index) {
+    int num_insert;
+    int num_shift;
+    int num_preshift;
+
+    // nothing to do if empty string is inserted or index invalid
+    if ((src->len == 0) || (index < 1) || (index > MAX_ANSISTRING_LENGTH)) {
+        return;
+    }
+
+    num_insert = src->len;
+    // number of chars from start of destination string to end of insertion
+    num_preshift = index - 1 + num_insert;
+
+    // don't overflow on insert
+    if (num_preshift > MAX_ANSISTRING_LENGTH) {
+        num_preshift = MAX_ANSISTRING_LENGTH;
+        num_insert = MAX_ANSISTRING_LENGTH - (index - 1);
+        num_shift = 0;
+    }
+    // shift trailing chars
+    else {
+        // number of bytes to be shifted
+        num_shift = dst->len - (index - 1);
+
+        if (num_shift > 0) {
+            // don't overflow when shifting
+            if (num_shift + num_preshift > MAX_ANSISTRING_LENGTH)
+                num_shift = MAX_ANSISTRING_LENGTH - num_preshift;
+
+            // time to move some bytes!
+            memmove(dst->str + num_preshift, dst->str + index - 1, num_shift);
+        }
+    }
+
+    // actual byte insertion
+    memmove(dst->str + index - 1, src->str, num_insert);
+    // store new length
+    dst->len = num_shift + num_preshift;
+}
+
 void __attribute__((overloadable)) fpcrtl_delete__vars(string255 *s, SizeInt index, SizeInt count) {
     // number of chars to be move
     int num_move;
--- a/project_files/hwc/rtl/system.h	Tue Oct 09 12:54:40 2018 -0400
+++ b/project_files/hwc/rtl/system.h	Thu Oct 11 23:43:31 2018 +0200
@@ -28,7 +28,9 @@
 /*
  * Insert a shortstring in another at a specified index
  */
-void        fpcrtl_insert__vars(string255 *src, string255 *dst, SizeInt index);
+void                                                        fpcrtl_insert__vars(string255 *src, string255 *dst, SizeInt index);
+void        __attribute__((overloadable))                   fpcrtl_insert__vars(astring *src, astring *dst, SizeInt index);
+
 #define     fpcrtl_insert(src, dst, index)                  fpcrtl_insert__vars(&(src), &(dst), index);
 #define     fpcrtl_Insert                                   fpcrtl_insert