Thread overview
templated find() working with static arrays, seeking help on.
Jul 15, 2006
David L. Davis
Jul 15, 2006
Oskar Linde
Jul 15, 2006
David L. Davis
Jul 01, 2007
Peter Neubauer
Re: templated find() working with static arrays - working example
Jul 15, 2006
David L. Davis
July 15, 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
July 15, 2006
In article <e9b555$19es$1@digitaldaemon.com>, David L. Davis says...
>
>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, ...

see www.csc.kth.se/~ol/array.d for an implementation of find that I believe works like you want.

I am typing this from a mobile phone so I cant comment on your code...

regards,
Oskar


July 15, 2006
In article <e9bfs2$1mo8$1@digitaldaemon.com>, Oskar Linde says...
>
>In article <e9b555$19es$1@digitaldaemon.com>, David L. Davis says...
>>
>>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, ...
>
>see www.csc.kth.se/~ol/array.d for an implementation of find that I believe works like you want.
>
>I am typing this from a mobile phone so I cant comment on your code...
>
>regards,
>Oskar

Oskar thanks!

I looked over your code...and WOW! I can barely follow / understand half of it with all those alias and such. But my eyes did fall on a "T.sizeof" in your code, which helped me solve my issue!

This is the fix I put in, it's not the sweetest piece of code...but it works. :)

# 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;
# }

Thanks again for replying,
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
July 15, 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
July 01, 2007
Oskar Linde Wrote:
> see www.csc.kth.se/~ol/array.d for an implementation of find that I believe works like you want.

That stuff is extremely useful for any project. Thanks!

-Peter