Skip to content
This repository
Browse code

add function Parrot_str_new_init and replace common parts of to_strin…

…g methods with a call to it
  • Loading branch information...
commit a0dbe1f7d0b30e85c2b478db3251b41b5d8ed557 1 parent 91518de
NotFound authored August 18, 2011
9  include/parrot/string_funcs.h
@@ -489,6 +489,13 @@ STRING * Parrot_str_iter_substr(PARROT_INTERP,
489 489
         __attribute__nonnull__(2)
490 490
         __attribute__nonnull__(3);
491 491
 
  492
+PARROT_WARN_UNUSED_RESULT
  493
+PARROT_CANNOT_RETURN_NULL
  494
+STRING * Parrot_str_new_from_cstring(PARROT_INTERP,
  495
+    ARGIN_NULLOK(const char *buffer),
  496
+    ARGIN_NULLOK(STRING *encodingname))
  497
+        __attribute__nonnull__(1);
  498
+
492 499
 #define ASSERT_ARGS_Parrot_str_bitwise_and __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
493 500
        PARROT_ASSERT_ARG(interp))
494 501
 #define ASSERT_ARGS_Parrot_str_bitwise_not __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
@@ -652,6 +659,8 @@ STRING * Parrot_str_iter_substr(PARROT_INTERP,
652 659
        PARROT_ASSERT_ARG(interp) \
653 660
     , PARROT_ASSERT_ARG(str) \
654 661
     , PARROT_ASSERT_ARG(l))
  662
+#define ASSERT_ARGS_Parrot_str_new_from_cstring __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
  663
+       PARROT_ASSERT_ARG(interp))
655 664
 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
656 665
 /* HEADERIZER END: src/string/api.c */
657 666
 
17  src/pmc/ptr.pmc
@@ -33,6 +33,8 @@ BEGIN_PMC_HEADER_PREAMBLE
33 33
 #define PTR_FAT_CLEAR(i, s) PObj_flag_CLEAR(private1, (s))
34 34
 END_PMC_HEADER_PREAMBLE
35 35
 
  36
+#include "parrot/string_funcs.h"
  37
+
36 38
 /* HEADERIZER HFILE: none */
37 39
 /* HEADERIZER BEGIN: static */
38 40
 /* HEADERIZER END: static */
@@ -184,21 +186,8 @@ specified. If the encoding is omitted or null, use platform encoding.
184 186
 */
185 187
 
186 188
     METHOD as_string(STRING *encodingname :optional) {
187  
-        STRING *result = STRINGNULL;
188 189
         const char * content = (const char *)SELF.get_pointer();
189  
-        if (content) {
190  
-            const STR_VTABLE *encoding = STRING_IS_NULL(encodingname) ?
191  
-                    Parrot_platform_encoding_ptr :
192  
-                    Parrot_find_encoding_by_string(INTERP, encodingname);
193  
-            if (encoding == NULL)
194  
-                Parrot_ex_throw_from_c_args(interp, NULL,
195  
-                        EXCEPTION_INVALID_ENCODING,
196  
-                        "Invalid encoding");
197  
-            else {
198  
-                int size = strlen(content);
199  
-                result = Parrot_str_new_init(INTERP, content, size, encoding, 0);
200  
-            }
201  
-        }
  190
+        STRING *result = Parrot_str_new_from_cstring(INTERP, content, encodingname);
202 191
         RETURN(STRING result);
203 192
     }
204 193
 
16  src/pmc/unmanagedstruct.pmc
@@ -22,6 +22,7 @@ bounds access will very likely segfault.
22 22
 */
23 23
 
24 24
 #include "parrot/compiler.h"
  25
+#include "parrot/string_funcs.h"
25 26
 
26 27
 /* HEADERIZER HFILE: none */
27 28
 /* HEADERIZER BEGIN: static */
@@ -1233,21 +1234,8 @@ specified. If the encoding is omitted or null, use platform encoding.
1233 1234
 */
1234 1235
 
1235 1236
     METHOD as_string(STRING *encodingname :optional) {
1236  
-        STRING *result = STRINGNULL;
1237 1237
         const char * content = (const char *)SELF.get_pointer();
1238  
-        if (content) {
1239  
-            const STR_VTABLE *encoding = STRING_IS_NULL(encodingname) ?
1240  
-                    Parrot_platform_encoding_ptr :
1241  
-                    Parrot_find_encoding_by_string(INTERP, encodingname);
1242  
-            if (encoding == NULL)
1243  
-                Parrot_ex_throw_from_c_args(interp, NULL,
1244  
-                        EXCEPTION_INVALID_ENCODING,
1245  
-                        "Invalid encoding");
1246  
-            else {
1247  
-                int size = strlen(content);
1248  
-                result = Parrot_str_new_init(INTERP, content, size, encoding, 0);
1249  
-            }
1250  
-        }
  1238
+        STRING *result = Parrot_str_new_from_cstring(INTERP, content, encodingname);
1251 1239
         RETURN(STRING result);
1252 1240
     }
1253 1241
 
45  src/string/api.c
@@ -629,15 +629,13 @@ Parrot_str_new_constant(PARROT_INTERP, ARGIN(const char *buffer))
629 629
     return s;
630 630
 }
631 631
 
632  
-
633 632
 /*
634 633
 
635 634
 =item C<STRING * Parrot_str_new_init(PARROT_INTERP, const char *buffer, UINTVAL
636 635
 len, const STR_VTABLE *encoding, UINTVAL flags)>
637 636
 
638  
-Given a buffer, its length, an encoding, a character set, and STRING flags,
639  
-creates and returns a new string. If buffer is NULL and len >= 0, allocates
640  
-len bytes.
  637
+Given a buffer, its length, an encoding, and STRING flags, creates and returns
  638
+a new string. If buffer is NULL and len >= 0, allocates len bytes.
641 639
 
642 640
 =cut
643 641
 
@@ -689,6 +687,45 @@ Parrot_str_new_init(PARROT_INTERP, ARGIN_NULLOK(const char *buffer), UINTVAL len
689 687
 
690 688
 /*
691 689
 
  690
+=item C<STRING * Parrot_str_new_from_cstring(PARROT_INTERP, const char *buffer,
  691
+STRING *encodingname)>
  692
+
  693
+Given a buffer and an encoding, creates and returns a new string. If buffer is
  694
+NULL the result is a null string. Otherwise, the buffer should be a zero
  695
+terminated c-style string and its content must be valid for the encoding
  696
+specified. If encoding is null, assume plaftorm encoding.
  697
+
  698
+=cut
  699
+
  700
+*/
  701
+
  702
+PARROT_WARN_UNUSED_RESULT
  703
+PARROT_CANNOT_RETURN_NULL
  704
+STRING *
  705
+Parrot_str_new_from_cstring(PARROT_INTERP, ARGIN_NULLOK(const char *buffer),
  706
+        ARGIN_NULLOK(STRING *encodingname))
  707
+{
  708
+    ASSERT_ARGS(Parrot_str_new_from_cstring)
  709
+    STRING *result = STRINGNULL;
  710
+    if (buffer) {
  711
+        const STR_VTABLE *encoding = STRING_IS_NULL(encodingname) ?
  712
+                Parrot_platform_encoding_ptr :
  713
+                Parrot_find_encoding_by_string(interp, encodingname);
  714
+        if (encoding == NULL)
  715
+            Parrot_ex_throw_from_c_args(interp, NULL,
  716
+                    EXCEPTION_INVALID_ENCODING,
  717
+                    "Invalid encoding");
  718
+        else {
  719
+            int size = strlen(buffer);
  720
+            result = Parrot_str_new_init(interp, buffer, size, encoding, 0);
  721
+        }
  722
+    }
  723
+    return result;
  724
+}
  725
+
  726
+
  727
+/*
  728
+
692 729
 =item C<STRING * Parrot_str_from_platform_cstring(PARROT_INTERP, const char *c)>
693 730
 
694 731
 Convert a C string, encoded in the platform's assumed encoding, to a Parrot

1 note on commit a0dbe1f

cotto
Owner

Thanks for factoring that out.

Please sign in to comment.
Something went wrong with that request. Please try again.