Switch to compiler-tester
[mono.git] / mono / metadata / locales.c
index 1f129cd9848480ea8e7ac2cd92d5d718eb8e88f0..2c8fdff303e31184b9ca4b29091eec39ffd00824 100644 (file)
@@ -43,8 +43,7 @@ static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
                                             gint32 count, gunichar2 value,
                                             MonoBoolean first);
 
-static MonoString *string_invariant_tolower (MonoString *this);
-static MonoString *string_invariant_toupper (MonoString *this);
+static const CultureInfoEntry* culture_info_entry_from_lcid (int lcid);
 
 static int
 culture_lcid_locator (const void *a, const void *b)
@@ -58,11 +57,11 @@ culture_lcid_locator (const void *a, const void *b)
 static int
 culture_name_locator (const void *a, const void *b)
 {
-       const CultureInfoNameEntry *aa = a;
+       const char *aa = a;
        const CultureInfoNameEntry *bb = b;
        int ret;
        
-       ret = strcmp (aa->name, bb->name);
+       ret = strcmp (aa, idx2string (bb->name));
 
        return ret;
 }
@@ -80,7 +79,7 @@ create_group_sizes_array (const gint *gs, gint ml)
        }
        
        ret = mono_array_new (mono_domain_get (),
-                       mono_defaults.int32_class, len);
+                       mono_get_int32_class (), len);
 
        for(i = 0; i < len; i++)
                mono_array_set (ret, gint32, i, gs [i]);
@@ -89,7 +88,7 @@ create_group_sizes_array (const gint *gs, gint ml)
 }
 
 static MonoArray*
-create_names_array (const gchar **names, int ml)
+create_names_array_idx (const guint16 *names, int ml)
 {
        MonoArray *ret;
        MonoDomain *domain;
@@ -101,15 +100,15 @@ create_names_array (const gchar **names, int ml)
        domain = mono_domain_get ();
 
        for (i = 0; i < ml; i++) {
-               if (names [i] == NULL)
+               if (names [i] == 0)
                        break;
                len++;
        }
 
-       ret = mono_array_new (mono_domain_get (), mono_defaults.string_class, len);
+       ret = mono_array_new (mono_domain_get (), mono_get_string_class (), len);
 
        for(i = 0; i < len; i++)
-               mono_array_set (ret, MonoString *, i, mono_string_new (domain, names [i]));
+               mono_array_set (ret, MonoString *, i, mono_string_new (domain, idx2string (names [i])));
 
        return ret;
 }
@@ -130,25 +129,33 @@ ves_icall_System_Globalization_CultureInfo_construct_datetime_format (MonoCultur
 
        domain = mono_domain_get ();
 
-       datetime->AbbreviatedDayNames = create_names_array (dfe->abbreviated_day_names,
+       datetime->AbbreviatedDayNames = create_names_array_idx (dfe->abbreviated_day_names,
                        NUM_DAYS);
-       datetime->AbbreviatedMonthNames = create_names_array (dfe->abbreviated_month_names,
+       datetime->AbbreviatedMonthNames = create_names_array_idx (dfe->abbreviated_month_names,
                        NUM_MONTHS);
-       datetime->AMDesignator = mono_string_new (domain, dfe->am_designator);
+       datetime->AMDesignator = mono_string_new (domain, idx2string (dfe->am_designator));
        datetime->CalendarWeekRule = dfe->calendar_week_rule;
-       datetime->DateSeparator = mono_string_new (domain, dfe->date_separator);
-       datetime->DayNames = create_names_array (dfe->day_names, NUM_DAYS);
+       datetime->DateSeparator = mono_string_new (domain, idx2string (dfe->date_separator));
+       datetime->DayNames = create_names_array_idx (dfe->day_names, NUM_DAYS);
        datetime->FirstDayOfWeek = dfe->first_day_of_week;
-       datetime->FullDateTimePattern = mono_string_new (domain, dfe->full_date_time_pattern);
-       datetime->LongDatePattern = mono_string_new (domain, dfe->long_date_pattern);
-       datetime->LongTimePattern = mono_string_new (domain, dfe->long_time_pattern);
-       datetime->MonthDayPattern = mono_string_new (domain, dfe->month_day_pattern);
-       datetime->MonthNames = create_names_array (dfe->month_names, NUM_MONTHS);
-       datetime->PMDesignator = mono_string_new (domain, dfe->pm_designator);
-       datetime->ShortDatePattern = mono_string_new (domain, dfe->short_date_pattern);
-       datetime->ShortTimePattern = mono_string_new (domain, dfe->short_time_pattern);
-       datetime->TimeSeparator = mono_string_new (domain, dfe->time_separator);
-       datetime->YearMonthPattern = mono_string_new (domain, dfe->year_month_pattern);
+       datetime->FullDateTimePattern = mono_string_new (domain, idx2string (dfe->full_date_time_pattern));
+       datetime->LongDatePattern = mono_string_new (domain, idx2string (dfe->long_date_pattern));
+       datetime->LongTimePattern = mono_string_new (domain, idx2string (dfe->long_time_pattern));
+       datetime->MonthDayPattern = mono_string_new (domain, idx2string (dfe->month_day_pattern));
+       datetime->MonthNames = create_names_array_idx (dfe->month_names, NUM_MONTHS);
+       datetime->PMDesignator = mono_string_new (domain, idx2string (dfe->pm_designator));
+       datetime->ShortDatePattern = mono_string_new (domain, idx2string (dfe->short_date_pattern));
+       datetime->ShortTimePattern = mono_string_new (domain, idx2string (dfe->short_time_pattern));
+       datetime->TimeSeparator = mono_string_new (domain, idx2string (dfe->time_separator));
+       datetime->YearMonthPattern = mono_string_new (domain, idx2string (dfe->year_month_pattern));
+       datetime->ShortDatePatterns = create_names_array_idx (dfe->short_date_patterns,
+                       NUM_SHORT_DATE_PATTERNS);
+       datetime->LongDatePatterns = create_names_array_idx (dfe->long_date_patterns,
+                       NUM_LONG_DATE_PATTERNS);
+       datetime->ShortTimePatterns = create_names_array_idx (dfe->short_time_patterns,
+                       NUM_SHORT_TIME_PATTERNS);
+       datetime->LongTimePatterns = create_names_array_idx (dfe->long_time_patterns,
+                       NUM_LONG_TIME_PATTERNS);
 
 }
 
@@ -170,39 +177,39 @@ ves_icall_System_Globalization_CultureInfo_construct_number_format (MonoCultureI
 
        number->currencyDecimalDigits = nfe->currency_decimal_digits;
        number->currencyDecimalSeparator = mono_string_new (domain,
-                       nfe->currency_decimal_separator);
+                       idx2string (nfe->currency_decimal_separator));
        number->currencyGroupSeparator = mono_string_new (domain,
-                       nfe->currency_group_separator);
+                       idx2string (nfe->currency_group_separator));
        number->currencyGroupSizes = create_group_sizes_array (nfe->currency_group_sizes,
                        GROUP_SIZE);
        number->currencyNegativePattern = nfe->currency_negative_pattern;
        number->currencyPositivePattern = nfe->currency_positive_pattern;
-       number->currencySymbol = mono_string_new (domain, nfe->currency_symbol);
-       number->naNSymbol = mono_string_new (domain, nfe->nan_symbol);
+       number->currencySymbol = mono_string_new (domain, idx2string (nfe->currency_symbol));
+       number->naNSymbol = mono_string_new (domain, idx2string (nfe->nan_symbol));
        number->negativeInfinitySymbol = mono_string_new (domain,
-                       nfe->negative_infinity_symbol);
-       number->negativeSign = mono_string_new (domain, nfe->negative_sign);
+                       idx2string (nfe->negative_infinity_symbol));
+       number->negativeSign = mono_string_new (domain, idx2string (nfe->negative_sign));
        number->numberDecimalDigits = nfe->number_decimal_digits;
        number->numberDecimalSeparator = mono_string_new (domain,
-                       nfe->number_decimal_separator);
-       number->numberGroupSeparator = mono_string_new (domain, nfe->number_group_separator);
+                       idx2string (nfe->number_decimal_separator));
+       number->numberGroupSeparator = mono_string_new (domain, idx2string (nfe->number_group_separator));
        number->numberGroupSizes = create_group_sizes_array (nfe->number_group_sizes,
                        GROUP_SIZE);
        number->numberNegativePattern = nfe->number_negative_pattern;
        number->percentDecimalDigits = nfe->percent_decimal_digits;
        number->percentDecimalSeparator = mono_string_new (domain,
-                       nfe->percent_decimal_separator);
+                       idx2string (nfe->percent_decimal_separator));
        number->percentGroupSeparator = mono_string_new (domain,
-                       nfe->percent_group_separator);
+                       idx2string (nfe->percent_group_separator));
        number->percentGroupSizes = create_group_sizes_array (nfe->percent_group_sizes,
                        GROUP_SIZE);
        number->percentNegativePattern = nfe->percent_negative_pattern;
        number->percentPositivePattern = nfe->percent_positive_pattern;
-       number->percentSymbol = mono_string_new (domain, nfe->percent_symbol);
-       number->perMilleSymbol = mono_string_new (domain, nfe->per_mille_symbol);
+       number->percentSymbol = mono_string_new (domain, idx2string (nfe->percent_symbol));
+       number->perMilleSymbol = mono_string_new (domain, idx2string (nfe->per_mille_symbol));
        number->positiveInfinitySymbol = mono_string_new (domain,
-                       nfe->positive_infinity_symbol);
-       number->positiveSign = mono_string_new (domain, nfe->positive_sign);
+                       idx2string (nfe->positive_infinity_symbol));
+       number->positiveSign = mono_string_new (domain, idx2string (nfe->positive_sign));
 }
 
 static MonoBoolean
@@ -211,23 +218,47 @@ construct_culture (MonoCultureInfo *this, const CultureInfoEntry *ci)
        MonoDomain *domain = mono_domain_get ();
 
        this->lcid = ci->lcid;
-       this->name = mono_string_new (domain, ci->name);
-       this->icu_name = mono_string_new (domain, ci->icu_name);
-       this->displayname = mono_string_new (domain, ci->displayname);
-       this->englishname = mono_string_new (domain, ci->englishname);
-       this->nativename = mono_string_new (domain, ci->nativename);
-       this->win3lang = mono_string_new (domain, ci->win3lang);
-       this->iso3lang = mono_string_new (domain, ci->iso3lang);
-       this->iso2lang = mono_string_new (domain, ci->iso2lang);
+       this->name = mono_string_new (domain, idx2string (ci->name));
+       this->icu_name = mono_string_new (domain, idx2string (ci->icu_name));
+       this->displayname = mono_string_new (domain, idx2string (ci->displayname));
+       this->englishname = mono_string_new (domain, idx2string (ci->englishname));
+       this->nativename = mono_string_new (domain, idx2string (ci->nativename));
+       this->win3lang = mono_string_new (domain, idx2string (ci->win3lang));
+       this->iso3lang = mono_string_new (domain, idx2string (ci->iso3lang));
+       this->iso2lang = mono_string_new (domain, idx2string (ci->iso2lang));
        this->parent_lcid = ci->parent_lcid;
        this->specific_lcid = ci->specific_lcid;
        this->datetime_index = ci->datetime_format_index;
        this->number_index = ci->number_format_index;
        this->calendar_data = ci->calendar_data;
+       this->text_info_data = &ci->text_info;
        
        return TRUE;
 }
 
+static gboolean
+construct_culture_from_specific_name (MonoCultureInfo *ci, gchar *name)
+{
+       const CultureInfoEntry *entry;
+       const CultureInfoNameEntry *ne;
+
+       MONO_ARCH_SAVE_REGS;
+
+       ne = bsearch (name, culture_name_entries, NUM_CULTURE_ENTRIES,
+                       sizeof (CultureInfoNameEntry), culture_name_locator);
+
+       if (ne == NULL)
+               return FALSE;
+
+       entry = &culture_entries [ne->culture_entry_index];
+
+       /* try avoiding another lookup, often the culture is its own specific culture */
+       if (entry->lcid != entry->specific_lcid)
+               entry = culture_info_entry_from_lcid (entry->specific_lcid);
+
+       return construct_culture (ci, entry);
+}
+
 static const CultureInfoEntry*
 culture_info_entry_from_lcid (int lcid)
 {
@@ -240,7 +271,7 @@ culture_info_entry_from_lcid (int lcid)
        return ci;
 }
 
-/**
+/*
  * The following two methods are modified from the ICU source code. (http://oss.software.ibm.com/icu)
  * Copyright (c) 1995-2003 International Business Machines Corporation and others
  * All rights reserved.
@@ -263,7 +294,7 @@ get_posix_locale (void)
 
        if ((strcmp ("C", posix_locale) == 0) || (strchr (posix_locale, ' ') != NULL)
                        || (strchr (posix_locale, '/') != NULL)) {
-               /**
+               /*
                 * HPUX returns 'C C C C C C C'
                 * Solaris can return /en_US/C/C/C/C/C on the second try.
                 * Maybe we got some garbage.
@@ -280,9 +311,7 @@ get_current_locale_name (void)
        gchar *locale;
        gchar *corrected = NULL;
        const gchar *p;
-       const gchar *q;
         gchar *c;
-       gint32 len;
 
 #ifdef PLATFORM_WIN32
        locale = g_win32_getlocale ();
@@ -307,7 +336,7 @@ get_current_locale_name (void)
        /* Note that we scan the *uncorrected* ID. */
        if ((p = strrchr (locale, '@')) != NULL) {
 
-               /**
+               /*
                 * In Mono we dont handle the '@' modifier because we do
                 * not have any cultures that use it. We just trim it
                 * off of the end of the name.
@@ -337,8 +366,7 @@ MonoBoolean
 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_current_locale (MonoCultureInfo *ci)
 {
        gchar *locale;
-       CultureInfoNameEntry key;
-       const CultureInfoNameEntry *ne;
+       gboolean ret;
 
        MONO_ARCH_SAVE_REGS;
 
@@ -346,16 +374,10 @@ ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_curren
        if (locale == NULL)
                return FALSE;
 
-       key.name = locale;
-       ne = bsearch (&key, culture_name_entries, NUM_CULTURE_ENTRIES,
-                       sizeof (CultureInfoNameEntry), culture_name_locator);
-
-        g_free (locale);
+       ret = construct_culture_from_specific_name (ci, locale);
+       g_free (locale);
 
-       if (ne == NULL)
-               return FALSE;
-
-       return construct_culture (ci, &culture_entries [ne->culture_entry_index]);
+       return ret;
 }
 
 MonoBoolean
@@ -377,19 +399,21 @@ MonoBoolean
 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (MonoCultureInfo *this,
                MonoString *name)
 {
-       CultureInfoNameEntry key;
        const CultureInfoNameEntry *ne;
+       char *n;
        
        MONO_ARCH_SAVE_REGS;
 
-       key.name = mono_string_to_utf8 (name);
-       ne = bsearch (&key, culture_name_entries, NUM_CULTURE_ENTRIES,
+       n = mono_string_to_utf8 (name);
+       ne = bsearch (n, culture_name_entries, NUM_CULTURE_ENTRIES,
                        sizeof (CultureInfoNameEntry), culture_name_locator);
 
-        g_free (key.name);
-
-       if (ne == NULL)
+       if (ne == NULL) {
+                /*g_print ("ne (%s) is null\n", n);*/
+               g_free (n);
                return FALSE;
+        }
+        g_free (n);
 
        return construct_culture (this, &culture_entries [ne->culture_entry_index]);
 }
@@ -398,28 +422,16 @@ MonoBoolean
 ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_specific_name (MonoCultureInfo *ci,
                MonoString *name)
 {
-       const CultureInfoEntry *entry;
-       CultureInfoNameEntry key;
-       const CultureInfoNameEntry *ne;
+       gchar *locale;
+       gboolean ret;
 
        MONO_ARCH_SAVE_REGS;
 
-       key.name = mono_string_to_utf8 (name);
-       ne = bsearch (&key, culture_name_entries, NUM_CULTURE_ENTRIES,
-                       sizeof (CultureInfoNameEntry), culture_name_locator);
-
-        g_free (key.name);
-
-       if (ne == NULL)
-               return FALSE;
-
-       entry = &culture_entries [ne->culture_entry_index];
-
-       /* try avoiding another lookup, often the culture is its own specific culture */
-       if (entry->lcid != entry->specific_lcid)
-               entry = culture_info_entry_from_lcid (entry->specific_lcid);
+       locale = mono_string_to_utf8 (name);
+       ret = construct_culture_from_specific_name (ci, locale);
+       g_free (locale);
 
-       return construct_culture (ci, entry);
+       return ret;
 }
 
 MonoArray*
@@ -446,14 +458,23 @@ ves_icall_System_Globalization_CultureInfo_internal_get_cultures (MonoBoolean ne
                        len++;
        }
 
-       class = mono_class_from_name (mono_defaults.corlib,
+       class = mono_class_from_name (mono_get_corlib (),
                        "System.Globalization", "CultureInfo");
+
+       /* The InvariantCulture is not in culture_entries */
+       /* We reserve the first slot in the array for it */
+       if (neutral)
+               len++;
+
        ret = mono_array_new (domain, class, len);
 
        if (len == 0)
                return ret;
 
        len = 0;
+       if (neutral)
+               mono_array_set (ret, MonoCultureInfo *, len++, NULL);
+
        for (i = 0; i < NUM_CULTURE_ENTRIES; i++) {
                ci = &culture_entries [i];
                is_neutral = ((ci->lcid & 0xff00) == 0 || ci->specific_lcid == 0);
@@ -469,6 +490,8 @@ ves_icall_System_Globalization_CultureInfo_internal_get_cultures (MonoBoolean ne
 }
 
 /**
+ * ves_icall_System_Globalization_CultureInfo_internal_is_lcid_neutral:
+ * 
  * Set is_neutral and return TRUE if the culture is found. If it is not found return FALSE.
  */
 MonoBoolean
@@ -552,7 +575,7 @@ static MonoArray *build_array (const UResourceBundle *bundle,
        subbundle=open_subbundle (bundle, resname, req_count);
        if(subbundle!=NULL) {
                arr=mono_array_new(mono_domain_get (),
-                                  mono_defaults.string_class, req_count);
+                                  mono_get_string_class (), req_count);
                
                for(i=0; i<req_count; i++) {
                        mono_array_set(arr, MonoString *, i, monostring_from_resource_index (subbundle, i));
@@ -571,7 +594,7 @@ static MonoDateTimeFormatInfo *create_DateTimeFormat (const char *locale)
        UResourceBundle *bundle, *subbundle;
        UErrorCode ec;
        
-       class=mono_class_from_name (mono_defaults.corlib,
+       class=mono_class_from_name (mono_get_corlib (),
                                    "System.Globalization",
                                    "DateTimeFormatInfo");
        new_dtf=(MonoDateTimeFormatInfo *)mono_object_new (mono_domain_get (),
@@ -654,11 +677,11 @@ static MonoNumberFormatInfo *create_NumberFormat (const char *locale)
        UResourceBundle *bundle, *subbundle, *table_entries;
        UErrorCode ec;
        int32_t count;
-       static char country [7]; //FIXME
+       static char country [7]; /* FIXME */
        const UChar *res_str;
        int32_t res_strlen;
 
-       class=mono_class_from_name (mono_defaults.corlib,
+       class=mono_class_from_name (mono_get_corlib (),
                                    "System.Globalization",
                                    "NumberFormatInfo");
        new_nf=(MonoNumberFormatInfo *)mono_object_new (mono_domain_get (),
@@ -814,7 +837,7 @@ void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoC
        icu_locale=mono_string_to_icu_locale (locale);
        if(icu_locale==NULL) {
                /* Something went wrong */
-               mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
+               mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
                return;
        }
        
@@ -876,7 +899,7 @@ void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompa
        icu_locale=mono_string_to_icu_locale (locale);
        if(icu_locale==NULL) {
                /* Something went wrong */
-               mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
+               mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
                return;
        }
 
@@ -1003,7 +1026,8 @@ gint32 ves_icall_System_Globalization_CompareInfo_internal_compare (MonoCompareI
                                                 len2, options));
        }
        
-       mono_monitor_try_enter ((MonoObject *)this, INFINITE);
+       if (!mono_monitor_enter ((MonoObject *)this))
+               return(-1);
        
        set_collator_options (coll, options);
                        
@@ -1027,6 +1051,7 @@ void ves_icall_System_Globalization_CompareInfo_free_internal_collator (MonoComp
        
        coll=this->ICU_collator;
        if(coll!=NULL) {
+               this->ICU_collator = NULL;
                ucol_close (coll);
        }
 }
@@ -1042,21 +1067,24 @@ void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo
        
        coll=this->ICU_collator;
        if(coll==NULL) {
-               mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "SystemException"));
+               mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
                return;
        }
        
-       mono_monitor_try_enter ((MonoObject *)this, INFINITE);
+       if (!mono_monitor_enter ((MonoObject *)this))
+               return;
        
        set_collator_options (coll, options);
 
-       keylen=ucol_getSortKey (coll, mono_string_chars (source), -1, NULL, 0);
+       keylen=ucol_getSortKey (coll, mono_string_chars (source),
+                               mono_string_length (source), NULL, 0);
        keybuf=g_malloc (sizeof(char)* keylen);
-       ucol_getSortKey (coll, mono_string_chars (source), -1, keybuf, keylen);
+       ucol_getSortKey (coll, mono_string_chars (source),
+                        mono_string_length (source), keybuf, keylen);
 
        mono_monitor_exit ((MonoObject *)this);
        
-       arr=mono_array_new (mono_domain_get (), mono_defaults.byte_class,
+       arr=mono_array_new (mono_domain_get (), mono_get_byte_class (),
                            keylen);
        for(i=0; i<keylen; i++) {
                mono_array_set (arr, guint8, i, keybuf[i]);
@@ -1086,7 +1114,7 @@ int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *
 #ifdef DEBUG
        g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", this->lcid);
 #endif
-       
+
        if(coll==NULL || this->lcid==0x007F ||
           options & CompareOptions_Ordinal) {
 #ifdef DEBUG
@@ -1106,7 +1134,8 @@ int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *
                        sizeof(UChar)*count);
        }
        
-       mono_monitor_try_enter ((MonoObject *)this, INFINITE);
+       if (!mono_monitor_enter ((MonoObject *)this))
+               return(-1);
        
        ec=U_ZERO_ERROR;
        
@@ -1126,9 +1155,10 @@ int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *
        /* Don't ignore some codepoints */
        ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
                           &ec);
-                       
-       search=usearch_openFromCollator (mono_string_chars (value), -1, usrcstr, -1, coll, NULL,
-                                        &ec);
+       
+       search=usearch_openFromCollator (mono_string_chars (value),
+                                        mono_string_length (value),
+                                        usrcstr, count, coll, NULL, &ec);
        if(U_SUCCESS (ec)) {
                if(first) {
                        pos=usearch_first (search, &ec);
@@ -1136,19 +1166,56 @@ int ves_icall_System_Globalization_CompareInfo_internal_index (MonoCompareInfo *
                        pos=usearch_last (search, &ec);
                }
 
-               if(pos!=USEARCH_DONE) {
+               while (pos!=USEARCH_DONE) {
+                       int32_t match_len;
+                       UChar *match;
+                       UCollationResult uret;
+                       
 #ifdef DEBUG
                        g_message (G_GNUC_PRETTY_FUNCTION
-                                  ": Got match at %d (sindex %d) len %d", pos,
-                                  sindex, usearch_getMatchedLength (search));
+                                  ": Got potential match at %d (sindex %d) len %d", pos, sindex, usearch_getMatchedLength (search));
 #endif
-                       if(sindex>0) {
-                               if(first) {
-                                       pos+=sindex;
-                               } else {
-                                       pos+=(sindex-count+1);
+
+                       /* ICU usearch currently ignores most of the
+                        * collator attributes :-(
+                        *
+                        * Check the returned match to see if it
+                        * really does match properly...
+                        */
+                       match_len = usearch_getMatchedLength (search);
+                       match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
+                       usearch_getMatchedText (search, match, match_len, &ec);
+
+                       uret = ucol_strcoll (coll, match, match_len,
+                                            mono_string_chars (value),
+                                            mono_string_length (value));
+                       g_free (match);
+                       
+                       if (uret == UCOL_EQUAL) {
+                               /* OK, we really did get a match */
+#ifdef DEBUG
+                               g_message (G_GNUC_PRETTY_FUNCTION
+                                          ": Got match at %d len %d", pos,
+                                          match_len);
+#endif
+
+                               if(sindex>0) {
+                                       if(first) {
+                                               pos+=sindex;
+                                       } else {
+                                               pos+=(sindex-count+1);
+                                       }
                                }
+
+                               break;
                        }
+
+                       /* False alarm, keep looking */
+                       if(first) {
+                               pos=usearch_next (search, &ec);
+                       } else {
+                               pos=usearch_previous (search, &ec);
+                       }       
                }
        } else {
                g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
@@ -1204,7 +1271,8 @@ int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareI
        }
        uvalstr[0]=value;
        
-       mono_monitor_try_enter ((MonoObject *)this, INFINITE);
+       if (!mono_monitor_enter ((MonoObject *)this))
+               return(-1);
        
        ec=U_ZERO_ERROR;
        
@@ -1225,8 +1293,8 @@ int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareI
        ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
                           &ec);
                        
-       search=usearch_openFromCollator (uvalstr, -1, usrcstr, -1, coll, NULL,
-                                        &ec);
+       search=usearch_openFromCollator (uvalstr, 1, usrcstr, count, coll,
+                                        NULL, &ec);
        if(U_SUCCESS (ec)) {
                if(first) {
                        pos=usearch_first (search, &ec);
@@ -1234,19 +1302,55 @@ int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareI
                        pos=usearch_last (search, &ec);
                }
 
-               if(pos!=USEARCH_DONE) {
+               while (pos!=USEARCH_DONE) {
+                       int32_t match_len;
+                       UChar *match;
+                       UCollationResult uret;
+                       
 #ifdef DEBUG
                        g_message (G_GNUC_PRETTY_FUNCTION
-                                  ": Got match at %d (sindex %d) len %d", pos,
-                                  sindex, usearch_getMatchedLength (search));
+                                  ": Got potential match at %d (sindex %d) len %d", pos, sindex, usearch_getMatchedLength (search));
+#endif
+
+                       /* ICU usearch currently ignores most of the
+                        * collator attributes :-(
+                        *
+                        * Check the returned match to see if it
+                        * really does match properly...
+                        */
+                       match_len = usearch_getMatchedLength (search);
+                       match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
+                       usearch_getMatchedText (search, match, match_len, &ec);
+
+                       uret = ucol_strcoll (coll, match, match_len, uvalstr,
+                                            1);
+                       g_free (match);
+                       
+                       if (uret == UCOL_EQUAL) {
+                               /* OK, we really did get a match */
+#ifdef DEBUG
+                               g_message (G_GNUC_PRETTY_FUNCTION
+                                          ": Got match at %d len %d", pos,
+                                          match_len);
 #endif
-                       if(sindex>0) {
-                               if(first) {
-                                       pos+=sindex;
-                               } else {
-                                       pos+=(sindex-count+1);
+
+                               if(sindex>0) {
+                                       if(first) {
+                                               pos+=sindex;
+                                       } else {
+                                               pos+=(sindex-count+1);
+                                       }
                                }
+
+                               break;
                        }
+
+                       /* False alarm, keep looking */
+                       if(first) {
+                               pos=usearch_next (search, &ec);
+                       } else {
+                               pos=usearch_previous (search, &ec);
+                       }       
                }
        } else {
                g_message (G_GNUC_PRETTY_FUNCTION ": usearch_open error: %s",
@@ -1257,7 +1361,7 @@ int ves_icall_System_Globalization_CompareInfo_internal_index_char (MonoCompareI
        
        mono_monitor_exit ((MonoObject *)this);
        
-       g_free (uvalstr);
+       g_free (usrcstr);
 
        return(pos);
 }
@@ -1296,7 +1400,8 @@ MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this,
                return(string_invariant_replace (this, old, new));
        }
        
-       mono_monitor_try_enter ((MonoObject *)comp, INFINITE);
+       if (!mono_monitor_enter ((MonoObject *)comp))
+               return(NULL);
        
        ec=U_ZERO_ERROR;
        
@@ -1317,9 +1422,11 @@ MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this,
        ucol_setAttribute (coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE,
                           &ec);
                        
-       search=usearch_openFromCollator (mono_string_chars (old), -1,
-                                        mono_string_chars (this), -1, coll,
-                                        NULL, &ec);
+       search=usearch_openFromCollator (mono_string_chars (old),
+                                        mono_string_length (old),
+                                        mono_string_chars (this),
+                                        mono_string_length (this),
+                                        coll, NULL, &ec);
        if(U_SUCCESS (ec)) {
                int pos, oldpos, len_delta=0;
                int32_t newstr_len=mono_string_length (new), match_len;
@@ -1335,10 +1442,17 @@ MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this,
                         * does match properly...
                         */
                        match_len = usearch_getMatchedLength (search);
+
+                       if(match_len == 0) {
+                               continue;
+                       }
+                       
                        match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
                        usearch_getMatchedText (search, match, match_len, &ec);
 
-                       if (ucol_strcoll (coll, match, -1, mono_string_chars (old), -1) == UCOL_EQUAL) {
+                       if (ucol_strcoll (coll, match, match_len,
+                                         mono_string_chars (old),
+                                         mono_string_length (old)) == UCOL_EQUAL) {
                                /* OK, we really did get a match */
 #ifdef DEBUG
                                g_message (G_GNUC_PRETTY_FUNCTION
@@ -1369,13 +1483,20 @@ MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this,
                    pos!=USEARCH_DONE;
                    pos=usearch_next (search, &ec)) {
                        match_len = usearch_getMatchedLength (search);
+
+                       if (match_len == 0) {
+                               continue;
+                       }
+                       
                        match=(UChar *)g_malloc0 (sizeof(UChar) * (match_len + 1));
                        usearch_getMatchedText (search, match, match_len, &ec);
 
                        /* Add the unmatched text */
                        u_strncat (uret, mono_string_chars (this)+oldpos,
                                   pos-oldpos);
-                       if (ucol_strcoll (coll, match, -1, mono_string_chars (old), -1) == UCOL_EQUAL) {
+                       if (ucol_strcoll (coll, match, match_len,
+                                         mono_string_chars (old),
+                                         mono_string_length (old)) == UCOL_EQUAL) {
                                /* Then the replacement */
                                u_strcat (uret, mono_string_chars (new));
                        } else {
@@ -1409,184 +1530,8 @@ MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this,
        return(ret);
 }
 
-MonoString *ves_icall_System_String_InternalToLower_Comp (MonoString *this, MonoCultureInfo *cult)
-{
-       MonoString *ret;
-       UChar *udest;
-       UErrorCode ec;
-       char *icu_loc;
-       int32_t len;
-
-       MONO_ARCH_SAVE_REGS;
-
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": [%s]",
-                  mono_string_to_utf8 (this));
-#endif
-
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", cult->lcid);
-#endif
-
-       icu_loc=mono_string_to_icu_locale (cult->icu_name);
-       if(icu_loc==NULL) {
-               mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
-               return(NULL);
-       }
-       
-       udest=(UChar *)g_malloc0 (sizeof(UChar)*(mono_string_length (this)+1));
-       
-       /* According to the docs, this might result in a longer or
-        * shorter string than we started with...
-        */
-
-       ec=U_ZERO_ERROR;
-       len=u_strToLower (udest, mono_string_length (this)+1,
-                         mono_string_chars (this), -1, icu_loc, &ec);
-       if(ec==U_BUFFER_OVERFLOW_ERROR ||
-          ec==U_STRING_NOT_TERMINATED_WARNING) {
-               g_free (udest);
-               udest=(UChar *)g_malloc0 (sizeof(UChar)*(len+1));
-               len=u_strToLower (udest, len+1, mono_string_chars (this), -1,
-                                 icu_loc, &ec);
-       }
-
-       if(U_SUCCESS (ec)) {
-               ret=mono_string_from_utf16 ((gunichar2 *)udest);
-       } else {
-               g_message (G_GNUC_PRETTY_FUNCTION ": u_strToLower error: %s",
-                          u_errorName (ec));
-               /* return something */
-               ret=this;
-       }
-       
-       g_free (icu_loc);
-       g_free (udest);
-       
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": returning [%s]",
-                  mono_string_to_utf8 (ret));
-#endif
-
-       return(ret);
-}
-
-MonoString *ves_icall_System_String_InternalToUpper_Comp (MonoString *this, MonoCultureInfo *cult)
-{
-       MonoString *ret;
-       UChar *udest;
-       UErrorCode ec;
-       char *icu_loc;
-       int32_t len;
-
-       MONO_ARCH_SAVE_REGS;
-
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": [%s]",
-                  mono_string_to_utf8 (this));
-#endif
-
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": LCID is %d", cult->lcid);
-#endif
-
-       icu_loc=mono_string_to_icu_locale (cult->icu_name);
-       if(icu_loc==NULL) {
-               mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
-               return(NULL);
-       }
-       
-       udest=(UChar *)g_malloc0 (sizeof(UChar)*(mono_string_length (this)+1));
-       
-       /* According to the docs, this might result in a longer or
-        * shorter string than we started with...
-        */
-
-       ec=U_ZERO_ERROR;
-       len=u_strToUpper (udest, mono_string_length (this)+1,
-                         mono_string_chars (this), -1, icu_loc, &ec);
-       if(ec==U_BUFFER_OVERFLOW_ERROR ||
-          ec==U_STRING_NOT_TERMINATED_WARNING) {
-               g_free (udest);
-               udest=(UChar *)g_malloc0 (sizeof(UChar)*(len+1));
-               len=u_strToUpper (udest, len+1, mono_string_chars (this), -1,
-                                 icu_loc, &ec);
-       }
-
-       if(U_SUCCESS (ec)) {
-               ret=mono_string_from_utf16 ((gunichar2 *)udest);
-       } else {
-               g_message (G_GNUC_PRETTY_FUNCTION ": u_strToUpper error: %s",
-                          u_errorName (ec));
-               /* return something */
-               ret=this;
-       }
-       
-       g_free (icu_loc);
-       g_free (udest);
-       
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": returning [%s]",
-                  mono_string_to_utf8 (ret));
-#endif
-       
-       return(ret);
-}
-
-gunichar2 ves_icall_System_Char_InternalToUpper_Comp (gunichar2 c, MonoCultureInfo *cult)
-{
-       UChar udest;
-       UErrorCode ec;
-       char *icu_loc;
-       int32_t len;
-       
-       MONO_ARCH_SAVE_REGS;
-
-       icu_loc=mono_string_to_icu_locale (cult->icu_name);
-       if(icu_loc==NULL) {
-               mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
-               return(0);
-       }
-       
-       ec=U_ZERO_ERROR;
-       len=u_strToUpper (&udest, 1, &c, 1, icu_loc, &ec);
-
-       if(U_SUCCESS (ec) && len==1) {
-               return udest;
-       } else {
-               /* return something */
-               return c;
-       }
-}
-
-
-gunichar2 ves_icall_System_Char_InternalToLower_Comp (gunichar2 c, MonoCultureInfo *cult)
-{
-       UChar udest;
-       UErrorCode ec;
-       char *icu_loc;
-       int32_t len;
-       
-       MONO_ARCH_SAVE_REGS;
-
-       icu_loc=mono_string_to_icu_locale (cult->icu_name);
-       if(icu_loc==NULL) {
-               mono_raise_exception ((MonoException *)mono_exception_from_name (mono_defaults.corlib, "System", "SystemException"));
-               return(0);
-       }
-       
-       ec=U_ZERO_ERROR;
-       len=u_strToLower (&udest, 1, &c, 1, icu_loc, &ec);
-
-       if(U_SUCCESS (ec) && len==1) {
-               return udest;
-       } else {
-               /* return something */
-               return c;
-       }
-}
-
 #else /* HAVE_ICU */
+
 void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoCultureInfo *this, MonoString *locale)
 {
        MONO_ARCH_SAVE_REGS;
@@ -1594,7 +1539,7 @@ void ves_icall_System_Globalization_CultureInfo_construct_internal_locale (MonoC
        /* Always claim "unknown locale" if we don't have ICU (only
         * called for non-invariant locales)
         */
-       mono_raise_exception((MonoException *)mono_exception_from_name(mono_defaults.corlib, "System", "ArgumentException"));
+       mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "ArgumentException"));
 }
 
 void ves_icall_System_Globalization_CompareInfo_construct_compareinfo (MonoCompareInfo *comp, MonoString *locale)
@@ -1627,7 +1572,7 @@ void ves_icall_System_Globalization_CompareInfo_assign_sortkey (MonoCompareInfo
        
        keylen=mono_string_length (source);
        
-       arr=mono_array_new (mono_domain_get (), mono_defaults.byte_class,
+       arr=mono_array_new (mono_domain_get (), mono_get_byte_class (),
                            keylen);
        for(i=0; i<keylen; i++) {
                mono_array_set (arr, guint8, i, mono_string_chars (source)[i]);
@@ -1669,35 +1614,6 @@ MonoString *ves_icall_System_String_InternalReplace_Str_Comp (MonoString *this,
        return(string_invariant_replace (this, old, new));
 }
 
-MonoString *ves_icall_System_String_InternalToLower_Comp (MonoString *this, MonoCultureInfo *cult)
-{
-       MONO_ARCH_SAVE_REGS;
-       
-       return(string_invariant_tolower (this));
-}
-
-MonoString *ves_icall_System_String_InternalToUpper_Comp (MonoString *this, MonoCultureInfo *cult)
-{
-       MONO_ARCH_SAVE_REGS;
-       
-       return(string_invariant_toupper (this));
-}
-
-gunichar2 ves_icall_System_Char_InternalToUpper_Comp (gunichar2 c, MonoCultureInfo *cult)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       return g_unichar_toupper (c);
-}
-
-
-gunichar2 ves_icall_System_Char_InternalToLower_Comp (gunichar2 c, MonoCultureInfo *cult)
-{
-       MONO_ARCH_SAVE_REGS;
-
-       return g_unichar_tolower (c);
-}
-
 #endif /* HAVE_ICU */
 
 static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
@@ -1706,28 +1622,28 @@ static gint32 string_invariant_compare_char (gunichar2 c1, gunichar2 c2,
        gint32 result;
        GUnicodeType c1type, c2type;
 
+       /* Ordinal can not be mixed with other options, and must return the difference, not only -1, 0, 1 */
+       if (options & CompareOptions_Ordinal) 
+               return (gint32) c1 - c2;
+       
        c1type = g_unichar_type (c1);
        c2type = g_unichar_type (c2);
-
+       
        if (options & CompareOptions_IgnoreCase) {
-               result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) - (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
-       } else if (options & CompareOptions_Ordinal) {
-               // Rotor/ms return the full value just not -1 and 1
-               return (gint32) c1 - c2;
+               result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) -
+                       (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
        } else {
-               /* No options. Kana, symbol and spacing options don't
+               /*
+                * No options. Kana, symbol and spacing options don't
                 * apply to the invariant culture.
                 */
-               if (c1type == G_UNICODE_UPPERCASE_LETTER &&
-                   c2type == G_UNICODE_LOWERCASE_LETTER) {
-                       return(1);
-               }
-                                       
-               if (c1type == G_UNICODE_LOWERCASE_LETTER &&
-                   c2type == G_UNICODE_UPPERCASE_LETTER) {
-                       return(-1);
-               }
-               
+
+               /*
+                * FIXME: here we must use the information from c1type and c2type
+                * to find out the proper collation, even on the InvariantCulture, the
+                * sorting is not done by computing the unicode values, but their
+                * actual sort order.
+                */
                result = (gint32) c1 - c2;
        }
 
@@ -1929,43 +1845,3 @@ static gint32 string_invariant_indexof_char (MonoString *source, gint32 sindex,
        }
 }
 
-static MonoString *string_invariant_tolower (MonoString *this)
-{
-       MonoString *ret;
-       gunichar2 *src; 
-       gunichar2 *dest;
-       gint32 i;
-
-       ret = mono_string_new_size(mono_domain_get (),
-                                  mono_string_length(this));
-
-       src = mono_string_chars (this);
-       dest = mono_string_chars (ret);
-
-       for (i = 0; i < mono_string_length (this); ++i) {
-               dest[i] = g_unichar_tolower(src[i]);
-       }
-
-       return(ret);
-}
-
-static MonoString *string_invariant_toupper (MonoString *this)
-{
-       MonoString *ret;
-       gunichar2 *src; 
-       gunichar2 *dest;
-       guint32 i;
-
-       ret = mono_string_new_size(mono_domain_get (),
-                                  mono_string_length(this));
-
-       src = mono_string_chars (this);
-       dest = mono_string_chars (ret);
-
-       for (i = 0; i < mono_string_length (this); ++i) {
-               dest[i] = g_unichar_toupper(src[i]);
-       }
-
-       return(ret);
-}
-