D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3371 - regexp behavior in console and win32 are different
Summary: regexp behavior in console and win32 are different
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-10-07 05:06 UTC by bcosca
Modified: 2015-06-09 01:26 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bcosca 2009-10-07 05:06:50 UTC
my win32 program has been driving me nuts until I realized that the following code:

/* --- console code --- */
import std.regexp;
import std.stdio;

void main() {
	auto r=search("abcdef","c");
	writefln(r[0]);
}
/* --- end of console code --- */

outputs "c" on the console. but the following win32 equivalent:

/* --- windows code --- */
import core.runtime;
import std.regexp;
import std.c.windows.windows;

extern(Windows) void WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {
	void exceptionHandler(Throwable e) {
		throw e;
	}
	try {
		Runtime.initialize(&exceptionHandler);
		auto r=search("abcdef","c");
		MessageBoxA(cast(HANDLE)0,cast(char*)r[0],"Alert",0);
		Runtime.terminate(&exceptionHandler);
	}
	catch(Object o) {
		MessageBoxA(cast(HANDLE)0,cast(LPSTR)o.toString(),"Alert",0);
	}
}
/* --- end of windows code --- */

results in "cdef"! also, the compiled win32 program - without the exception handling code - causes windows to crash.
Comment 1 Jarrett Billingsley 2009-10-07 06:16:54 UTC
Uh, no. Windows uses 0-terminated strings, D does not. All you're seeing when you output the result of the search with the message box is Windows stupidly reading until it hits a nul character (which, thankfully, D inserts at the end of string literals, or else you'd probably be getting a segfault here). You should be using toStringz to convert any D strings to C strings.