July 20, 2010 Re: Higher level built-in strings | ||||
|---|---|---|---|---|
| ||||
I did not read all the discussion in detail, but in my opinion something that would be very useful in a library is
struct String{
void *ptr;
size_t _l;
enum :size_t {
MaskLen=((~cast(size_t)0)>>2)
}
enum :int {
BitsLen=8*size_t.sizeof-2
}
size_t len(){
return (_l & MaskLen);
}
int encodingId(){
return cast(int)(_l>>BitsLen);
}
}
plus stuff to simplify its creation from T[] arrays and getting T[] arrays from it.
this type would them be used where one wants a string without caring about its encoding, and without having to make all string accepting functions templates.
As it was explained by others many string operations are rather generic.
*this* is what I would have expected from string, not an alias to char[].
Fawzi
| ||||
July 21, 2010 Re: Higher level built-in strings | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | "Walter Bright" <newshound2@digitalmars.com> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ: news:i24st1$12uh$1@digitalmars.com... > Jerome M. Berger wrote: >> And what about this one: >> >> void func(T) (T range) { >> foreach (elem; range) >> assert (is (typeof (elem) == ElementType!(T))); >> } >> >> func ("azerty"); >> auto a = [ 1, 2, 3, 4, 5]; >> func (a); > > You can specialize the template for strings: > > void func(T:string)(T range) { ... } > Hmm. Theoreticaly a bit more general void func(T, U, V )(T rangeT, U rangeU, V rangeV) { ... } void func(T:string, U, V )(T rangeT, U rangeU, V rangeV) { ... } void func(T, U:string, V )(T rangeT, U rangeU, V rangeV) { ... } void func(T, U, V:string )(T rangeT, U rangeU, V rangeV) { ... } void func(T:string, U:string, V )(T rangeT, U rangeU, V rangeV) { ... } void func(T:string, U, V:string )(T rangeT, U rangeU, V rangeV) { ... } void func(T, U:string, V:string )(T rangeT, U rangeU, V rangeV) { ... } void func(T:string, U:string, V:string )(T rangeT, U rangeU, V rangeV) { ... } | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply