project_files/hwc/rtl/system.c
changeset 10838 2abe883c9c21
parent 10625 125e120165aa
child 10840 6d1986411733
--- a/project_files/hwc/rtl/system.c	Sun Mar 01 15:04:31 2015 +0100
+++ b/project_files/hwc/rtl/system.c	Sun Mar 01 15:56:00 2015 +0100
@@ -73,6 +73,46 @@
     return result;
 }
 
+void fpcrtl_insert__vars(string255 *src, string255 *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 > 255)) {
+        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 > 255) {
+        num_insert = 255 - (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 > 255)
+                num_shift = 255 - 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;