View previous topic :: View next topic |
Author |
Message |
teqdruid
Joined: 11 May 2004 Posts: 390 Location: UMD
|
Posted: Sat Dec 25, 2004 1:50 am Post subject: String Interface |
|
|
Regardless of whether or not you like the idea of a String class, I think one must agree that for those who want to use a String class, it's imperative that there be either one common String class, or a decent String interface. I think the latter is the way to go. As such, I've put together a very basic interface, IString. I'd like to revise it here, then present it to Walter and ask him to include it with Phobos, so people can standardize on it.
Code: | interface IString
{
uint toHash();
int opCmp(Object object);
int opEquals(Object o);
char opIndex(uint index);
IString opCat(IString other);
IString opCat(char[] other);
IString opSlice(int start, int end);
char[] toString();
char* toCString();
uint length();
char charAt();
IString dup();
IString substring(uint start, uint end);
IString substring(uint start);
IString trim();
IString toLower();
IString toUpper();
IString[] split(char sep);
int compare(IString other);
int compareIgnoreCase(IString other);
int find(IString other, uint start=0);
int rfind(IString other, uint start=uint.max);
int indexOf(char c, uint start=0);
int lastIndexOf(char c, uint start=uint.max);
bit startsWith(IString other);
bit endsWitch(IString other);
bit contains(IString other);
} |
It's just what I came up with in a few minutes. It also assumes that one wants to work with chars... there's probably a better (probably template based) way to deal with this, but I don't know how the template-interface stuff works (if at all).
What'cha y'all think?
John |
|
Back to top |
|
|
andy
Joined: 15 Mar 2004 Posts: 71
|
Posted: Tue Dec 28, 2004 10:37 pm Post subject: |
|
|
I think it's a bad idea: the D standard library contains classes and functions for dealing with D concepts.
A string class/interface is not a D concept. Trying to cram it in is about as straightforward and elegant as writing C-styled code in Java.
(whether or not it should be so is subject to debate, but the odds of winning such a debate when it counts are rather slim ) _________________ "Complacency is a far more dangerous attitude than outrage." - Naomi Littlebear |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Wed Dec 29, 2004 12:35 am Post subject: |
|
|
I agree with Andy's perspective.
To get such an Interface adopted you (a) have to make it worthwhile, and (b) apply OO efforts toward a library other than Phobos.
Mango.icu.String leverages the vast Unicode ICU library in an attempt to get some traction (though I don't know of even one person using it), and an Interface around that has been discussed before. Mango.icu has also been offered to the Ares library for consideration.
Phobos is, uhhh ... interesting. Anyone with an eye for OOP will have to look elsewhere. |
|
Back to top |
|
|
teqdruid
Joined: 11 May 2004 Posts: 390 Location: UMD
|
Posted: Wed Dec 29, 2004 1:41 am Post subject: |
|
|
So without some sort of standard interface for strings in the standard library, how does one prevent issues with different libraries using different string classes? I'll soon be running into this issue with Mango and Dui. Dui uses it's own String class, and the mango.icu stuff uses UString. If both implement the interface, and have constructors that use the interface, then it makes everything a whole lot easier for me. |
|
Back to top |
|
|
andy
Joined: 15 Mar 2004 Posts: 71
|
Posted: Wed Dec 29, 2004 2:34 am Post subject: |
|
|
The only thing I can suggest is to use char[] (or wchar[] or dchar[]) everywhere and only construct UString/dool.Strings when forced to at gunpoint, as it were.
I vaguely recall non-OO ICU bindings being worked on at some point. Working with them would probably be nicer, especially given the funny thing D does with functions that recieve an array as their first argument.
Code: | // The first argument of this function is an array, so
void foo(T[] x, char[] y) { ... }
T[] t = ...;
char[] s = ...;
// we can either use it like this
foo(t, s);
// or this (!!)
t.foo(s); |
_________________ "Complacency is a far more dangerous attitude than outrage." - Naomi Littlebear |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Wed Dec 29, 2004 11:12 am Post subject: |
|
|
teqDruid wrote: | So without some sort of standard interface for strings in the standard library, how does one prevent issues with different libraries using different string classes? I'll soon be running into this issue with Mango and Dui. Dui uses it's own String class, and the mango.icu stuff uses UString. If both implement the interface, and have constructors that use the interface, then it makes everything a whole lot easier for me. |
Ant and I talked about making our stuff compatable via an interface. Currently, I understand that the ball is in Ant's court ... he's been pretty busy with other stuff though.
I agree that something needs to be done, and soon. |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Wed Dec 29, 2004 11:16 am Post subject: |
|
|
andy wrote: | I vaguely recall non-OO ICU bindings being worked on at some point. Working with them would probably be nicer, especially given the funny thing D does with functions that recieve an array as their first argument. |
I'm confused, Andy ... can you provide examples whereby the application of OO is detrimental (or otherwise unwarranted) where something like ICU is concerned? My experience is otherwise. |
|
Back to top |
|
|
teqdruid
Joined: 11 May 2004 Posts: 390 Location: UMD
|
Posted: Wed Dec 29, 2004 11:19 am Post subject: |
|
|
Quote: | Ant and I talked about making our stuff compatable via an interface. |
Good news for DUI and Mango users, but you can't hope to collaborate with all the string class writers should the community grow much more. |
|
Back to top |
|
|
jcc7
Joined: 22 Feb 2004 Posts: 657 Location: Muskogee, OK, USA
|
Posted: Wed Dec 29, 2004 10:13 pm Post subject: |
|
|
teqDruid wrote: | Quote: | Ant and I talked about making our stuff compatable via an interface. |
Good news for DUI and Mango users, but you can't hope to collaborate with all the string class writers should the community grow much more. | IMO, it's not good news yet for DUI/Mango users until Ant and Kris actually reach a compromise. |
|
Back to top |
|
|
andy
Joined: 15 Mar 2004 Posts: 71
|
Posted: Fri Dec 31, 2004 4:04 pm Post subject: |
|
|
kris wrote: | andy wrote: | I vaguely recall non-OO ICU bindings being worked on at some point. Working with them would probably be nicer, especially given the funny thing D does with functions that recieve an array as their first argument. |
I'm confused, Andy ... can you provide examples whereby the application of OO is detrimental (or otherwise unwarranted) where something like ICU is concerned? My experience is otherwise. | The application of OO itself isn't detrimental at all so much as the fact that D's lack of implicit conversion places severe limits on the ways differing data types can interact.
Those limitations effectively doom any string interface to being clunky at best: all conversions to and from have to be explicit, which can lead to an awful lot of noise that doesn't actually convey any meaning, but is required regardless. C++ still suffers all kinds of silly issues revolving around this, despite the fact that it does allow implicit conversions.
Preferring the 'plain-old-data' string types over interfaces leads to fewer fights with the language, and, perhaps most importantly, a single, simple unified string. (except not, because of D's handling of ?char[] overloads with respect to string literals, but that's a fight for another day.....) _________________ "Complacency is a far more dangerous attitude than outrage." - Naomi Littlebear |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Fri Dec 31, 2004 5:26 pm Post subject: |
|
|
andy wrote: | The application of OO itself isn't detrimental at all so much as the fact that D's lack of implicit conversion places severe limits on the ways differing data types can interact |
It is a bit finicky. Sticking with, say, wchar[] as the internal format does tend to eliminate a lot of the noise you speak of (the ICU wrappers have minimal toX() conversions and/or constructors), but then induces limitations in other ways. There's unlikely to be ever a perfect solution in such cases, which is sad.
On the other hand, using a String class rather than just an array permits the addition of useful meta-data; such as COW controls. Oh well ... I guess we have to live with D as it is.
Cheers! |
|
Back to top |
|
|
|