Author Topic: explode_list() and item_list() augmentations  (Read 1745 times)

Offline z993126

  • BFF
  • ***
  • Posts: 128
    • View Profile
explode_list() and item_list() augmentations
« on: May 26, 2012, 04:28:11 PM »
addition of "or" and "nor" as elements in addition to "and" to /secure/sefun/english.c.  for item_list(), the conjunction may be passed as the last argument, either if the first argument is an array and the conjunction is separate (e.g. ( ({"apple","banana","turnip"}),"or" )), or if each word is a separate argument (e.g. ( "apple","banana", "turnip", "or" ) ).

Code: [Select]
string array explode_list( string list ){
string array items;
string one, two;

list = lower_case( list );
if( sscanf( list, "%s and %s", one, two ) == 2 || sscanf( list, "%s or %s", one, two ) == 2 ){
items = explode( one, "," ) + explode( two, "," );
}else{
items = explode( list, "," );
}
items = map( items, function( string str ){
if( !str ){ return 0; }
str = trim( str );
if( strlen( str ) > 4 && str[0..3] == "and " ){
str = str[4..];
}else if( strlen( str ) > 3 && str[0..2] == "or " ){
str = str[3..];
}else if( strlen( str ) > 4 && str[0..3] == "nor " ){
str = str[4..];
}
str = remove_article( str );
if( strlen( str ) > 3 && str[0..2] == "my " ){ str = str[3..]; }
return str;
} );
return filter( items, (: $1 && $1 != "" :) );
}

varargs string item_list( mixed array items... ){
mapping list = ([]);
string str;
int maxi;
string seperator = "and", *seperators = ({ "and", "or", "nor" });

if( !sizeof( items ) ){ error("No argument 1 to item_list().\n"); }
if( arrayp( items[0] ) ){
if( !sizeof( items[0] ) ){ return ""; }
if( sizeof( items ) > 1 && stringp( items[<1] ) ){
if( member_array( items[<1], seperators ) > -1 ){ seperator = items[<1]; }
}
items = items[0];
}else{
if( stringp( items[<1] ) && member_array( items[<1], seperators ) > -1 ){
seperator = items[<1];
items = items[0..<2];
}
}
foreach( mixed value in items ){
if( objectp( value ) ){
if( living( value ) ){
value = value->GetName();
}else{
value = value->GetShort();
}
}
if( !value ){ continue; }
if( !list[value] ){
list[value] = 1;
}else{
list[value]++;
}
}
maxi = sizeof( items = keys( list ) );
if( maxi < 1 ){ return ""; }
str = consolidate( list[items[0]], items[0] );
if( maxi == 1 ) { return str; }
if( maxi > 2 ){ str += ","; }
for( int i = 1; i < maxi; i++ ){
if( i == maxi-1 ){
str += " " + seperator + " ";
}else{
str += " ";
}
str += consolidate( list[items[i]], items[i] );
if( i < maxi-1 ){ str += ","; }
}
return str;
}