Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 NotFound authored
9 include/parrot/string_funcs.h
View
@@ -489,6 +489,13 @@ STRING * Parrot_str_iter_substr(PARROT_INTERP,
__attribute__nonnull__(2)
__attribute__nonnull__(3);
+PARROT_WARN_UNUSED_RESULT
+PARROT_CANNOT_RETURN_NULL
+STRING * Parrot_str_new_from_cstring(PARROT_INTERP,
+ ARGIN_NULLOK(const char *buffer),
+ ARGIN_NULLOK(STRING *encodingname))
+ __attribute__nonnull__(1);
+
#define ASSERT_ARGS_Parrot_str_bitwise_and __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_Parrot_str_bitwise_not __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
@@ -652,6 +659,8 @@ STRING * Parrot_str_iter_substr(PARROT_INTERP,
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(str) \
, PARROT_ASSERT_ARG(l))
+#define ASSERT_ARGS_Parrot_str_new_from_cstring __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: src/string/api.c */
17 src/pmc/ptr.pmc
View
@@ -33,6 +33,8 @@ BEGIN_PMC_HEADER_PREAMBLE
#define PTR_FAT_CLEAR(i, s) PObj_flag_CLEAR(private1, (s))
END_PMC_HEADER_PREAMBLE
+#include "parrot/string_funcs.h"
+
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
@@ -184,21 +186,8 @@ specified. If the encoding is omitted or null, use platform encoding.
*/
METHOD as_string(STRING *encodingname :optional) {
- STRING *result = STRINGNULL;
const char * content = (const char *)SELF.get_pointer();
- if (content) {
- const STR_VTABLE *encoding = STRING_IS_NULL(encodingname) ?
- Parrot_platform_encoding_ptr :
- Parrot_find_encoding_by_string(INTERP, encodingname);
- if (encoding == NULL)
- Parrot_ex_throw_from_c_args(interp, NULL,
- EXCEPTION_INVALID_ENCODING,
- "Invalid encoding");
- else {
- int size = strlen(content);
- result = Parrot_str_new_init(INTERP, content, size, encoding, 0);
- }
- }
+ STRING *result = Parrot_str_new_from_cstring(INTERP, content, encodingname);
RETURN(STRING result);
}
16 src/pmc/unmanagedstruct.pmc
View
@@ -22,6 +22,7 @@ bounds access will very likely segfault.
*/
#include "parrot/compiler.h"
+#include "parrot/string_funcs.h"
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
@@ -1233,21 +1234,8 @@ specified. If the encoding is omitted or null, use platform encoding.
*/
METHOD as_string(STRING *encodingname :optional) {
- STRING *result = STRINGNULL;
const char * content = (const char *)SELF.get_pointer();
- if (content) {
- const STR_VTABLE *encoding = STRING_IS_NULL(encodingname) ?
- Parrot_platform_encoding_ptr :
- Parrot_find_encoding_by_string(INTERP, encodingname);
- if (encoding == NULL)
- Parrot_ex_throw_from_c_args(interp, NULL,
- EXCEPTION_INVALID_ENCODING,
- "Invalid encoding");
- else {
- int size = strlen(content);
- result = Parrot_str_new_init(INTERP, content, size, encoding, 0);
- }
- }
+ STRING *result = Parrot_str_new_from_cstring(INTERP, content, encodingname);
RETURN(STRING result);
}
45 src/string/api.c
View
@@ -629,15 +629,13 @@ Parrot_str_new_constant(PARROT_INTERP, ARGIN(const char *buffer))
return s;
}
-
/*
=item C<STRING * Parrot_str_new_init(PARROT_INTERP, const char *buffer, UINTVAL
len, const STR_VTABLE *encoding, UINTVAL flags)>
-Given a buffer, its length, an encoding, a character set, and STRING flags,
-creates and returns a new string. If buffer is NULL and len >= 0, allocates
-len bytes.
+Given a buffer, its length, an encoding, and STRING flags, creates and returns
+a new string. If buffer is NULL and len >= 0, allocates len bytes.
=cut
@@ -689,6 +687,45 @@ Parrot_str_new_init(PARROT_INTERP, ARGIN_NULLOK(const char *buffer), UINTVAL len
/*
+=item C<STRING * Parrot_str_new_from_cstring(PARROT_INTERP, const char *buffer,
+STRING *encodingname)>
+
+Given a buffer and an encoding, creates and returns a new string. If buffer is
+NULL the result is a null string. Otherwise, the buffer should be a zero
+terminated c-style string and its content must be valid for the encoding
+specified. If encoding is null, assume plaftorm encoding.
+
+=cut
+
+*/
+
+PARROT_WARN_UNUSED_RESULT
+PARROT_CANNOT_RETURN_NULL
+STRING *
+Parrot_str_new_from_cstring(PARROT_INTERP, ARGIN_NULLOK(const char *buffer),
+ ARGIN_NULLOK(STRING *encodingname))
+{
+ ASSERT_ARGS(Parrot_str_new_from_cstring)
+ STRING *result = STRINGNULL;
+ if (buffer) {
+ const STR_VTABLE *encoding = STRING_IS_NULL(encodingname) ?
+ Parrot_platform_encoding_ptr :
+ Parrot_find_encoding_by_string(interp, encodingname);
+ if (encoding == NULL)
+ Parrot_ex_throw_from_c_args(interp, NULL,
+ EXCEPTION_INVALID_ENCODING,
+ "Invalid encoding");
+ else {
+ int size = strlen(buffer);
+ result = Parrot_str_new_init(interp, buffer, size, encoding, 0);
+ }
+ }
+ return result;
+}
+
+
+/*
+
=item C<STRING * Parrot_str_from_platform_cstring(PARROT_INTERP, const char *c)>
Convert a C string, encoded in the platform's assumed encoding, to a Parrot

1 comment on commit a0dbe1f

cotto
Owner

Thanks for factoring that out.

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