# HG changeset patch # User sheepluva # Date 1425221760 -3600 # Node ID 2abe883c9c21b9ee1c91b1550e3daf54d0f13f04 # Parent 1b7a4d3111ea07c92d054ffa27f27ab8f712bdc6 teach pas2c how to insert() shortstrings diff -r 1b7a4d3111ea -r 2abe883c9c21 hedgewars/pas2cRedo.pas --- a/hedgewars/pas2cRedo.pas Sun Mar 01 15:04:31 2015 +0100 +++ b/hedgewars/pas2cRedo.pas Sun Mar 01 15:56:00 2015 +0100 @@ -66,12 +66,12 @@ Now : function : integer; - new, dispose, FillChar, Move : procedure; + new, dispose, FillChar, Insert, Delete, Move : procedure; trunc, round : function : integer; abs, sqr : function : integer; - StrPas, FormatDateTime, copy, delete, str, PosS, trim, LowerCase : function : shortstring; + StrPas, FormatDateTime, copy, str, PosS, trim, LowerCase : function : shortstring; pos : function : integer; StrToInt : function : integer; SetLength, SetLengthA, val, StrDispose, StrCopy : procedure; diff -r 1b7a4d3111ea -r 2abe883c9c21 project_files/hwc/rtl/system.c --- 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; diff -r 1b7a4d3111ea -r 2abe883c9c21 project_files/hwc/rtl/system.h --- a/project_files/hwc/rtl/system.h Sun Mar 01 15:04:31 2015 +0100 +++ b/project_files/hwc/rtl/system.h Sun Mar 01 15:56:00 2015 +0100 @@ -26,6 +26,13 @@ astring fpcrtl_copyA(astring s, Integer Index, Integer Count); /* + * Insert a shortstring in another at a specified index + */ +void fpcrtl_insert__vars(string255 *src, string255 *dst, SizeInt index); +#define fpcrtl_insert(src, dst, index) fpcrtl_insert__vars(&(src), &(dst), index); +#define fpcrtl_Insert fpcrtl_insert + +/* * Delete removes Count characters from string S, starting at position Index. * All characters after the deleted characters are shifted Count positions to the left, * and the length of the string is adjusted. @@ -33,6 +40,7 @@ #define fpcrtl_delete(s, index, count) fpcrtl_delete__vars(&(s), index, count) void __attribute__((overloadable)) fpcrtl_delete__vars(string255 *s, SizeInt index, SizeInt count); void __attribute__((overloadable)) fpcrtl_delete__vars(astring *s, SizeInt index, SizeInt count); +#define fpcrtl_Delete fpcrtl_delete string255 fpcrtl_floatToStr(double n);