www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - very newbie question (sring confusion)

reply "Lloyd Dupont" <ld-REMOVE galador.net> writes:
maybe it's late, or I have a hard time reading the string and array class 
methods.. (which I have! :~)
anyway, I have a very simple problem and it seems hard to fix!

let say I have a list of resource named preferential order (favored first) 
and I'd like to do return a list of match in preferential order (with always 
the default last)


for example:
my resources (default first): "en-AU", "fr-FR", "fr-BE"
my matches: for xxxx i'd like to return a variable list / array xxx
"en-US" => "en-AU"
"fr-CA"  => "fr-FR", "en-AU"
"fr-BE"  => "fr-BE", "en-AU"
"es-EP" => "en-AU"

how could I do that? 
Jun 03 2011
parent reply "Lloyd Dupont" <ld-REMOVE galador.net> writes:
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;
}
Jun 03 2011
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Lloyd Dupont:

 I did the following, what do you think of my implementation?
Those for loops seem better as foreach ones, or even reverse foreach ones. Probably in std.algorithm there is stuff to shorten your code. Bye, bearophile
Jun 03 2011
parent "Lloyd Dupont" <ld-REMOVE galador.net> writes:
std.algorithm!
will have a look, thanks!

"bearophile"  wrote in message news:isb5ql$1i23$1 digitalmars.com... 

Lloyd Dupont:

 I did the following, what do you think of my implementation?
Those for loops seem better as foreach ones, or even reverse foreach ones. Probably in std.algorithm there is stuff to shorten your code. Bye, bearophile
Jun 03 2011
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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
Jun 03 2011