Skip to content

Commit aa48d5e

Browse files
committed
Add simpler exported unescape function to librubyparser
* Moves logic from the C extension to librubyparser which can be shared with the Fiddle backend.
1 parent 2667b68 commit aa48d5e

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

ext/yarp/extension.c

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -529,30 +529,16 @@ named_captures(VALUE self, VALUE source) {
529529
// version.
530530
static VALUE
531531
unescape(VALUE source, yp_unescape_type_t unescape_type) {
532-
yp_string_t string;
533-
VALUE result;
532+
yp_string_t result;
534533

535-
yp_list_t error_list;
536-
yp_list_init(&error_list);
537-
538-
const char *start = RSTRING_PTR(source);
539-
size_t length = RSTRING_LEN(source);
540-
541-
yp_parser_t parser;
542-
yp_parser_init(&parser, start, length, "");
543-
544-
yp_unescape_manipulate_string(&parser, start, length, &string, unescape_type, &error_list);
545-
if (yp_list_empty_p(&error_list)) {
546-
result = rb_str_new(yp_string_source(&string), yp_string_length(&string));
534+
if (yp_unescape_string(RSTRING_PTR(source), RSTRING_LEN(source), unescape_type, &result)) {
535+
VALUE str = rb_str_new(yp_string_source(&result), yp_string_length(&result));
536+
yp_string_free(&result);
537+
return str;
547538
} else {
548-
result = Qnil;
539+
yp_string_free(&result);
540+
return Qnil;
549541
}
550-
551-
yp_string_free(&string);
552-
yp_list_free(&error_list);
553-
yp_parser_free(&parser);
554-
555-
return result;
556542
}
557543

558544
// Do not unescape anything in the given string. This is here to provide a

include/yarp/unescape.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ typedef enum {
3333
// given unescape mode.
3434
YP_EXPORTED_FUNCTION void yp_unescape_manipulate_string(yp_parser_t *parser, const char *value, size_t length, yp_string_t *string, yp_unescape_type_t unescape_type, yp_list_t *error_list);
3535

36+
// Accepts a source string and a type of unescaping and returns the unescaped version.
37+
// The caller must yp_string_free(result); after calling this function.
38+
YP_EXPORTED_FUNCTION bool yp_unescape_string(const char *start, size_t length, yp_unescape_type_t unescape_type, yp_string_t *result);
39+
3640
YP_EXPORTED_FUNCTION size_t yp_unescape_calculate_difference(const char *value, const char *end, yp_unescape_type_t unescape_type, bool expect_single_codepoint, yp_list_t *error_list);
3741

3842
#endif

src/unescape.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "yarp/unescape.h"
1+
#include "yarp.h"
22

33
/******************************************************************************/
44
/* Character checks */
@@ -528,6 +528,25 @@ yp_unescape_manipulate_string(yp_parser_t *parser, const char *value, size_t len
528528
string->as.owned.length = dest_length + ((size_t) (end - cursor));
529529
}
530530

531+
YP_EXPORTED_FUNCTION bool
532+
yp_unescape_string(const char *start, size_t length, yp_unescape_type_t unescape_type, yp_string_t *result) {
533+
bool success;
534+
535+
yp_list_t error_list;
536+
yp_list_init(&error_list);
537+
538+
yp_parser_t parser;
539+
yp_parser_init(&parser, start, length, "");
540+
541+
yp_unescape_manipulate_string(&parser, start, length, result, unescape_type, &error_list);
542+
success = yp_list_empty_p(&error_list);
543+
544+
yp_list_free(&error_list);
545+
yp_parser_free(&parser);
546+
547+
return success;
548+
}
549+
531550
// This function is similar to yp_unescape_manipulate_string, except it doesn't
532551
// actually perform any string manipulations. Instead, it calculates how long
533552
// the unescaped character is, and returns that value

0 commit comments

Comments
 (0)