Thread overview
std.string.replaceSlice() causes a 'Access Violation Error'
May 30, 2005
David L. Davis
May 30, 2005
Derek Parnell
May 30, 2005
David L. Davis
May 30, 2005
# // replaceSlice() causes a "Access Violation Error"
# // WinXP SP2, dmd v0.125
# private import std.string;
# private import std.stdio;
#
# int main()
# {
#     char[] s = "1234567890";
#     char[] sx;
#     sx = replaceSlice(s, "67", "xxxx");
#
#     writefln("s=\"%s\", sx=\"%s\"", s, sx);
#     return 0;
# }

Output:
--------
C:\dmd>dmd error1.d
C:\dmd\bin\..\..\dm\bin\link.exe error1,,,user32+kernel32/noi;

C:\dmd>error1
Error: Access Violation

C:\dmd>

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
May 30, 2005
On Mon, 30 May 2005 19:53:46 +0000 (UTC), David L. Davis wrote:

> # // replaceSlice() causes a "Access Violation Error"
> # // WinXP SP2, dmd v0.125
> # private import std.string;
> # private import std.stdio;
> #
> # int main()
> # {
> #     char[] s = "1234567890";
> #     char[] sx;
> #     sx = replaceSlice(s, "67", "xxxx");
> #
> #     writefln("s=\"%s\", sx=\"%s\"", s, sx);
> #     return 0;
> # }

This function must work with a *slice* of the first string parameter and not just a substring.

For example ...

  char[] ss;
  int pos;
  ss = "67";
  pos = find(s, ss);
  if (pos >= 0)
     sx = replaceSlice(s, s[pos..pos+ss.length], "xxxx")

would work.

The code for this function is also a poor example of DbC, in that it uses an 'in' block to validate input data. So when the library is compiled in release mode, coders who use the library do not benefit from the validation check. Had the library not been compiled with -release, or the function use the body block to validate input data, you *may* have seen an exception occur. I say 'may', because the validation is not complete anyway, as certain conditions can still get through it.


-- 
Derek Parnell
Melbourne, Australia
31/05/2005 7:08:04 AM
May 30, 2005
In article <deh3vcn8a47t$.l93c6iajf2pf.dlg@40tude.net>, Derek Parnell says...
>
>On Mon, 30 May 2005 19:53:46 +0000 (UTC), David L. Davis wrote:
>
>> # // replaceSlice() causes a "Access Violation Error"
>> # // WinXP SP2, dmd v0.125
>> # private import std.string;
>> # private import std.stdio;
>> #
>> # int main()
>> # {
>> #     char[] s = "1234567890";
>> #     char[] sx;
>> #     sx = replaceSlice(s, "67", "xxxx");
>> #
>> #     writefln("s=\"%s\", sx=\"%s\"", s, sx);
>> #     return 0;
>> # }
>
>This function must work with a *slice* of the first string parameter and not just a substring.
>
>For example ...
>
>  char[] ss;
>  int pos;
>  ss = "67";
>  pos = find(s, ss);
>  if (pos >= 0)
>     sx = replaceSlice(s, s[pos..pos+ss.length], "xxxx")
>
>would work.
>
>The code for this function is also a poor example of DbC, in that it uses an 'in' block to validate input data. So when the library is compiled in release mode, coders who use the library do not benefit from the validation check. Had the library not been compiled with -release, or the function use the body block to validate input data, you *may* have seen an exception occur. I say 'may', because the validation is not complete anyway, as certain conditions can still get through it.
>
>
>-- 
>Derek Parnell
>Melbourne, Australia
>31/05/2005 7:08:04 AM

Thanks for the reply Derek, and for pointing out my mistake with a clear sample. :)

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html