Permalink
Browse files

Initial implementation of the foldcase operation for strings, in purs…

…uit of GH #786. moritz++ for the suggestion.
  • Loading branch information...
Whiteknight committed Jun 9, 2012
1 parent 2c97f1c commit e0dd1ada6ab099d5a396241745f6b251ffc105f8
View
@@ -71,6 +71,7 @@ void Parrot_x_panic_and_exit(
unsigned int line);
PARROT_COLD
+PARROT_NO_ADDRESS_SAFETY_ANALYSIS
void Parrot_x_execute_on_exit_handlers(PARROT_INTERP, int status)
__attribute__nonnull__(1);
View
@@ -48,6 +48,7 @@
#define STRING_upcase(interp, src) ((src)->encoding)->upcase((interp), (src))
#define STRING_downcase(interp, src) ((src)->encoding)->downcase((interp), (src))
#define STRING_titlecase(interp, src) ((src)->encoding)->titlecase((interp), (src))
+#define STRING_foldcase(interp, src) ((src)->encoding)->foldcase((interp), (src))
#define STRING_upcase_first(interp, src) ((src)->encoding)->upcase_first((interp), (src))
#define STRING_downcase_first(interp, src) ((src)->encoding)->downcase_first((interp), (src))
#define STRING_titlecase_first(interp, src) ((src)->encoding)->titlecase_first((interp), (src))
@@ -114,6 +115,7 @@ typedef STRING * (*str_vtable_decompose_t)(PARROT_INTERP, ARGIN(const STRING *sr
typedef STRING * (*str_vtable_upcase_t)(PARROT_INTERP, ARGIN(const STRING *src));
typedef STRING * (*str_vtable_downcase_t)(PARROT_INTERP, ARGIN(const STRING *src));
typedef STRING * (*str_vtable_titlecase_t)(PARROT_INTERP, ARGIN(const STRING *src));
+typedef STRING * (*str_vtable_foldcase_t)(PARROT_INTERP, ARGIN(const STRING *src));
typedef STRING * (*str_vtable_upcase_first_t)(PARROT_INTERP, ARGIN(const STRING *src));
typedef STRING * (*str_vtable_downcase_first_t)(PARROT_INTERP, ARGIN(const STRING *src));
typedef STRING * (*str_vtable_titlecase_first_t)(PARROT_INTERP, ARGIN(const STRING *src));
@@ -160,6 +162,7 @@ struct _str_vtable {
str_vtable_upcase_t upcase;
str_vtable_downcase_t downcase;
str_vtable_titlecase_t titlecase;
+ str_vtable_foldcase_t foldcase;
str_vtable_upcase_first_t upcase_first;
str_vtable_downcase_first_t downcase_first;
str_vtable_titlecase_first_t titlecase_first;
@@ -432,6 +432,7 @@ static STR_VTABLE Parrot_ascii_encoding = {
ascii_upcase,
ascii_downcase,
ascii_titlecase,
+ (str_vtable_foldcase_t)ascii_downcase,
ascii_upcase_first,
ascii_downcase_first,
ascii_titlecase_first,
@@ -230,6 +230,7 @@ static STR_VTABLE Parrot_binary_encoding = {
(str_vtable_upcase_t)binary_error,
(str_vtable_downcase_t)binary_error,
(str_vtable_titlecase_t)binary_error,
+ (str_vtable_foldcase_t)binary_error,
(str_vtable_upcase_first_t)binary_error,
(str_vtable_downcase_first_t)binary_error,
(str_vtable_titlecase_first_t)binary_error,
@@ -460,6 +460,7 @@ static STR_VTABLE Parrot_latin1_encoding = {
latin1_upcase,
latin1_downcase,
latin1_titlecase,
+ (str_vtable_foldcase_t)latin1_downcase,
latin1_upcase_first,
latin1_downcase_first,
latin1_titlecase_first,
@@ -168,6 +168,7 @@ static STR_VTABLE Parrot_null_encoding = {
(str_vtable_upcase_t)null_error,
(str_vtable_downcase_t)null_error,
(str_vtable_titlecase_t)null_error,
+ (str_vtable_foldcase_t)null_error,
(str_vtable_upcase_first_t)null_error,
(str_vtable_downcase_first_t)null_error,
(str_vtable_titlecase_first_t)null_error,
@@ -1376,6 +1376,7 @@ unicode_compose(PARROT_INTERP, ARGIN(const STRING *src))
#define ENCODING_UPCASE 1
#define ENCODING_DOWNCASE 2
#define ENCODING_TITLECASE 3
+#define ENCODING_FOLDCASE 4
/*
@@ -1413,6 +1414,11 @@ convert_case_buf(PARROT_INTERP, ARGMOD_NULLOK(char *dest_buf), size_t dest_len,
(const UChar *)src_buf, src_len / 2,
NULL, NULL, &err);
break;
+ case ENCODING_FOLDCASE:
+ res = u_strFoldCase((UChar *)dest_buf, dest_len / 2,
+ (const UChar *)src_buf, src_len / 2,
+ U_FOLD_CASE_DEFAULT, &err);
+ break;
default:
res = 0; /* Should never happen, just to avoid a warning */
break;
@@ -1576,6 +1582,25 @@ unicode_titlecase(PARROT_INTERP, ARGIN(const STRING *src))
return unicode_convert_case(interp, src, ENCODING_TITLECASE);
}
+/*
+
+=item C<STRING * unicode_foldcase(PARROT_INTERP, const STRING *src)>
+
+Perform a Fold-Case on the string
+
+=cut
+
+*/
+
+PARROT_CANNOT_RETURN_NULL
+STRING *
+unicode_foldcase(PARROT_INTERP, ARGIN(const STRING *src))
+{
+ ASSERT_ARGS(unicode_foldcase)
+ /* This forces an up-convertion to utf16, which might not be desirable
+ in all cases */
+ return unicode_convert_case(interp, src, ENCODING_FOLDCASE);
+}
/*
@@ -256,6 +256,11 @@ PARROT_CANNOT_RETURN_NULL
STRING* unicode_downcase_first(PARROT_INTERP, const STRING *src)
__attribute__nonnull__(1);
+PARROT_CANNOT_RETURN_NULL
+STRING * unicode_foldcase(PARROT_INTERP, ARGIN(const STRING *src))
+ __attribute__nonnull__(1)
+ __attribute__nonnull__(2);
+
PARROT_CANNOT_RETURN_NULL
STRING* unicode_titlecase(PARROT_INTERP, ARGIN(const STRING *src))
__attribute__nonnull__(1)
@@ -377,6 +382,9 @@ STRING* unicode_upcase_first(PARROT_INTERP, const STRING *src)
, PARROT_ASSERT_ARG(src))
#define ASSERT_ARGS_unicode_downcase_first __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
+#define ASSERT_ARGS_unicode_foldcase __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp) \
+ , PARROT_ASSERT_ARG(src))
#define ASSERT_ARGS_unicode_titlecase __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(src))
@@ -434,6 +434,7 @@ static STR_VTABLE Parrot_ucs2_encoding = {
unicode_upcase,
unicode_downcase,
unicode_titlecase,
+ unicode_foldcase,
unicode_upcase_first,
unicode_downcase_first,
unicode_titlecase_first,
@@ -440,6 +440,7 @@ static STR_VTABLE Parrot_ucs4_encoding = {
unicode_upcase,
unicode_downcase,
unicode_titlecase,
+ unicode_foldcase,
unicode_upcase_first,
unicode_downcase_first,
unicode_titlecase_first,
@@ -614,6 +614,7 @@ static STR_VTABLE Parrot_utf16_encoding = {
unicode_upcase,
unicode_downcase,
unicode_titlecase,
+ unicode_foldcase,
unicode_upcase_first,
unicode_downcase_first,
unicode_titlecase_first,
@@ -615,6 +615,7 @@ static STR_VTABLE Parrot_utf8_encoding = {
unicode_upcase,
unicode_downcase,
unicode_titlecase,
+ unicode_foldcase,
unicode_upcase_first,
unicode_downcase_first,
unicode_titlecase_first,

0 comments on commit e0dd1ad

Please sign in to comment.