# HG changeset patch # User unc0rr # Date 1539294211 -7200 # Node ID 5f819b90d479ddd16833cf50f825bb3b8a1667c5 # Parent 6b2c87490f0a5de04a8512d901dacdf234aa576c - Fix insert() for strings in pas2c - Implement insert() for ansistrings diff -r 6b2c87490f0a -r 5f819b90d479 hedgewars/uLocale.pas --- 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; diff -r 6b2c87490f0a -r 5f819b90d479 project_files/hwc/rtl/pas2c.h --- 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; diff -r 6b2c87490f0a -r 5f819b90d479 project_files/hwc/rtl/system.c --- 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; diff -r 6b2c87490f0a -r 5f819b90d479 project_files/hwc/rtl/system.h --- 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