88 // number of chars from start of destination string to end of insertion |
88 // number of chars from start of destination string to end of insertion |
89 num_preshift = index - 1 + num_insert; |
89 num_preshift = index - 1 + num_insert; |
90 |
90 |
91 // don't overflow on insert |
91 // don't overflow on insert |
92 if (num_preshift > 255) { |
92 if (num_preshift > 255) { |
|
93 num_preshift = 255; |
93 num_insert = 255 - (index - 1); |
94 num_insert = 255 - (index - 1); |
94 num_shift = 0; |
95 num_shift = 0; |
95 } |
96 } |
96 // shift trailing chars |
97 // shift trailing chars |
97 else { |
98 else { |
100 |
101 |
101 if (num_shift > 0) { |
102 if (num_shift > 0) { |
102 // don't overflow when shifting |
103 // don't overflow when shifting |
103 if (num_shift + num_preshift > 255) |
104 if (num_shift + num_preshift > 255) |
104 num_shift = 255 - num_preshift; |
105 num_shift = 255 - num_preshift; |
|
106 |
|
107 // time to move some bytes! |
|
108 memmove(dst->str + num_preshift, dst->str + index - 1, num_shift); |
|
109 } |
|
110 } |
|
111 |
|
112 // actual byte insertion |
|
113 memmove(dst->str + index - 1, src->str, num_insert); |
|
114 // store new length |
|
115 dst->len = num_shift + num_preshift; |
|
116 } |
|
117 |
|
118 void __attribute__((overloadable)) fpcrtl_insert__vars(astring *src, astring *dst, SizeInt index) { |
|
119 int num_insert; |
|
120 int num_shift; |
|
121 int num_preshift; |
|
122 |
|
123 // nothing to do if empty string is inserted or index invalid |
|
124 if ((src->len == 0) || (index < 1) || (index > MAX_ANSISTRING_LENGTH)) { |
|
125 return; |
|
126 } |
|
127 |
|
128 num_insert = src->len; |
|
129 // number of chars from start of destination string to end of insertion |
|
130 num_preshift = index - 1 + num_insert; |
|
131 |
|
132 // don't overflow on insert |
|
133 if (num_preshift > MAX_ANSISTRING_LENGTH) { |
|
134 num_preshift = MAX_ANSISTRING_LENGTH; |
|
135 num_insert = MAX_ANSISTRING_LENGTH - (index - 1); |
|
136 num_shift = 0; |
|
137 } |
|
138 // shift trailing chars |
|
139 else { |
|
140 // number of bytes to be shifted |
|
141 num_shift = dst->len - (index - 1); |
|
142 |
|
143 if (num_shift > 0) { |
|
144 // don't overflow when shifting |
|
145 if (num_shift + num_preshift > MAX_ANSISTRING_LENGTH) |
|
146 num_shift = MAX_ANSISTRING_LENGTH - num_preshift; |
105 |
147 |
106 // time to move some bytes! |
148 // time to move some bytes! |
107 memmove(dst->str + num_preshift, dst->str + index - 1, num_shift); |
149 memmove(dst->str + num_preshift, dst->str + index - 1, num_shift); |
108 } |
150 } |
109 } |
151 } |