templated find() working with static arrays - working example
David L. Davis
SpottedTiger at yahoo.com
Sat Jul 15 14:17:43 PDT 2006
I thought for completeness, I'd post the full working example code. Just in case
someone might find it useful now, or in the future.
# // 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);
#
# static if (!is(T == char) && !is(T == wchar) && !is(T == dchar) )
# {
# // Set iLen for a char[x] static array
# const int iLen = T.sizeof; //v.length; <-need to use this value
#
# // Set iLenW for a wchar[x] static array
# static if (T.sizeof >= 2)
# const int iLenW = T.sizeof / 2;
# else
# const int iLenW = 0;
#
# // Set iLenW for a dchar[x] static array
# static if (T.sizeof >= 4)
# const int iLenD = T.sizeof / 4;
# else
# const int iLenD = 0;
# }
# else
# {
# const int iLen = 0;
# const int iLenW = 0;
# const int iLenD = 0;
# }
#
# debug(string) writef("ti.toString=%s, typeid(T)=%s, ",
# ti.toString, typeid(T));
#
# 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[iLenW]))
# {
# debug(string) writefln("wchar[] section called!");
# return std.utf.toUTF8(v);
# }
# else static if (is(T == dchar[]) || is(T == dchar[iLenD]))
# {
# 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:
-------------------------
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], char[] section called!
ti.toString=char[], typeid(T)=char[2], char[] section called!
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], char[] section called!
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], wchar[] section called!
char[] sx1="123456", char[] sx2="45", find()=3
ti.toString=char[], typeid(T)=char[], char[] section called!
ti.toString=wchar[], typeid(T)=wchar[2], wchar[] section called!
char[] sx1="2345", char[] sx2="45", find()=2
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>
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