templated find() working with static arrays, seeking help on.
David L. Davis
SpottedTiger at yahoo.com
Sat Jul 15 09:29:25 PDT 2006
I'm trying to build a module of templated std.string functions that allows any
character (char, wchar, or dchar) or string (void[], char[], wchar[], or
dchar[]) as a parameters, which I'm current testing first with the
std.string.find() function. And after a lot of pain, I'm like 90% in getting
this to work. But currently I'm still trying to work pass the "Literal String /
Static Array" issue where I need to get this line "const int iLen = v.length;"
to set correctly. So, if anyone can help point to a way to solve this, it would
be great (also today my birthday, and having been working on this darn thing for
months off and on...it would be a great gift if someone could solve ths last
remaining issue ;) ).
# // Template find
# // Allows char, wchar, dchar, void[], char[], wchar[], and dchar[]
# module findtem;
#
# debug = string;
#
# debug(string) private import std.stdio;
# private import std.utf;
# private import std.string;
#
# // Returns the true type of the value.
# public TypeInfo gettype(...)
# {
# TypeInfo ti = _arguments[0];
# _arguments.length = 0;
# _argptr = null;
# return ti;
# }
#
# char[] toUTF8(void[] vs) { return cast(char[])vs; }
#
# char[] toUTF8(char c)
# {
# char[] s = new char[1];
# s[0] = c;
# return s;
# }
#
# char[] toUTF8(in wchar wc)
# {
# wchar[] ws = new wchar[1];
# ws[0] = wc;
# return std.utf.toUTF8(ws);
# }
#
# char[] toUTF8(in dchar dc)
# {
# dchar[] ds = new dchar[1];
# ds[0] = dc;
# return std.utf.toUTF8(ds);
# }
#
# template getCharA(T)
# {
# char[] getCharA(T v)
# {
# TypeInfo ti = gettype(v);
# debug(string) writef("ti.toString=%s, typeid(T)=%s, ",
# ti.toString, typeid(T));
#
# static if (!is(T == char) && !is(T == wchar) && !is(T == dchar) )
# const int iLen = 6; //v.length; <-need to use this value
# else
# const int iLen = 0;
#
# static if (is(T == char))
# {
# debug(string) writefln("char section called!");
# return findtem.toUTF8(v);
# }
# else static if (is(T == wchar))
# {
# debug(string) writefln("wchar section called!");
# return findtem.toUTF8(v);
# }
# else static if (is(T == dchar))
# {
# debug(string) writefln("dchar section called!");
# return findtem.toUTF8(v);
# }
# else static if (is(T == char[]) || is(T == char[iLen]))
# {
# debug(string) writefln("char[] section called!");
# return v;
# }
# else static if (is(T == void[]) || is(T == void[iLen]))
# {
# debug(string) writefln("void[] section called!");
# return cast(char[])v;
# }
# else static if (is(T == wchar[]) || is(T == wchar[iLen]))
# {
# debug(string) writefln("wchar[] section called!");
# return std.utf.toUTF8(v);
# }
# else static if (is(T == dchar[]) || is(T == dchar[iLen]))
# {
# debug(string) writefln("dchar[] called!");
# return std.utf.toUTF8(v);
# }
# else
# {
# debug(string) writefln("string literal falling thru!");
# return findtem.toUTF8(v);
# }
# }
# }
#
# template find(T1, T2)
# {
# int find(in T1 s1, in T2 s2)
# {
# char[] sx1 = getCharA(s1);
# char[] sx2 = getCharA(s2);
# int i;
#
# i = std.string.find(sx1, sx2);
# debug(string) writefln("%s sx1=\"%s\", %s sx2=\"%s\", find()=%d",
# typeid(typeof(sx1)), sx1,
# typeid(typeof(sx2)), sx2, i);
# debug(string) writefln();
# return i;
# }
# }
#
# debug(string)
# {
# int main()
# {
# void[] vs = "56";
# char[] s = "2345";
# wchar[] ws = "123";
# int i;
#
# i = find("23456"c, "34");
# i = find("234567", vs);
# i = find("234567"w, vs);
# i = find("234567"d, vs);
# i = find("abc", 'b');
# i = find('c', 'c');
# i = find("123456"w, "45"w);
# i = find(s, "45"w);
# i = find(ws, ws);
# return 0;
# }
# }
Output setting iLen to a static 6, would like it to be dynmanic:
----------------------------------------------------------------
C:\dmd>dmd findtest1.d
C:\dmd\bin\..\..\dm\bin\link.exe findtest1,,,user32+kernel32/noi;
C:\dmd>findtest1
ti.toString=char[], typeid(T)=char[5], string literal falling thru!
ti.toString=char[], typeid(T)=char[2], string literal falling thru!
char[] sx1="23456", char[] sx2="34", find()=1
ti.toString=char[], typeid(T)=char[6], char[] section called!
ti.toString=void[], typeid(T)=void[], void[] section called!
char[] sx1="234567", char[] sx2="56", find()=3
ti.toString=wchar[], typeid(T)=wchar[6], wchar[] section called!
ti.toString=void[], typeid(T)=void[], void[] section called!
char[] sx1="234567", char[] sx2="56", find()=3
ti.toString=dchar[], typeid(T)=dchar[6], dchar[] called!
ti.toString=void[], typeid(T)=void[], void[] section called!
char[] sx1="234567", char[] sx2="56", find()=3
ti.toString=char[], typeid(T)=char[3], string literal falling thru!
ti.toString=char, typeid(T)=char, char section called!
char[] sx1="abc", char[] sx2="b", find()=1
ti.toString=char, typeid(T)=char, char section called!
ti.toString=char, typeid(T)=char, char section called!
char[] sx1="c", char[] sx2="c", find()=0
ti.toString=wchar[], typeid(T)=wchar[6], wchar[] section called!
ti.toString=wchar[], typeid(T)=wchar[2], string literal falling thru!
char[] sx1="123456", char[] sx2="4 5 ", find()=-1
ti.toString=char[], typeid(T)=char[], char[] section called!
ti.toString=wchar[], typeid(T)=wchar[2], string literal falling thru!
char[] sx1="2345", char[] sx2="4 5 ", find()=-1
ti.toString=wchar[], typeid(T)=wchar[], wchar[] section called!
ti.toString=wchar[], typeid(T)=wchar[], wchar[] section called!
char[] sx1="123", char[] sx2="123", find()=0
C:\dmd>
Thanks in advance for any help given,
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
More information about the Digitalmars-d-learn
mailing list