very newbie question (sring confusion)
Jonathan M Davis
jmdavisProg at gmx.com
Fri Jun 3 10:42:05 PDT 2011
On 2011-06-03 09:55, Lloyd Dupont wrote:
> I did the following, what do you think of my implementation?
> (checking if my string and array usage could be improved / powered
> up!!!????)
>
> string[] _locales = ["en-AU", "fr-FR"];
> string getCurrentLocal() { return "fr-BE"; }
> string[] getCandidates()
> {
> auto local = getCurrentLocal();
>
> string match = null;
> for (int i = _locales.length; i-->0;)
> {
> if(_locales[i] == local)
> {
> match = _locales[i];
> break;
> }
> }
>
> string partial = null;
> if(local.length >= 2 && match == null)
> {
> for (int i = _locales.length; i-->0;)
> {
> auto tmpl = _locales[i];
> if (tmpl.length > 2 && tmpl[0] == local[0] && tmpl[1] ==
> local[1])
> {
> partial = tmpl;
> break;
> }
> }
> }
>
> string[] result;
> if(match)
> {
> result.length = result.length + 1;
> result[result.length-1] = match;
> }
> if(partial && partial != match)
> {
> result.length = result.length + 1;
> result[result.length-1] = partial;
> }
> if(match != _locales[0] && partial != _locales[0])
> {
> result.length = result.length + 1;
> result[result.length-1] = _locales[0];
> }
> return result;
> }
You should probably take a look at std.algorithm.find.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list