Skip to content

Commit e9a6291

Browse files
authored
style: cleanup xml_xpath_context.c (#3383)
**What problem is this PR intended to solve?** I'm trying to establish a naming convention that has good debug stack walkback and helps readers know what's in file scope and what's more broadly callable. Maybe I'm failing. Dunno. **Have you included adequate test coverage?** N/A **Does this change affect the behavior of either the C or the Java implementations?** N/A
2 parents d36f453 + 3cf2e45 commit e9a6291

File tree

1 file changed

+58
-68
lines changed

1 file changed

+58
-68
lines changed

ext/nokogiri/xml_xpath_context.c

Lines changed: 58 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ static const xmlChar *NOKOGIRI_BUILTIN_PREFIX = (const xmlChar *)"nokogiri-built
1212
static const xmlChar *NOKOGIRI_BUILTIN_URI = (const xmlChar *)"https://www.nokogiri.org/default_ns/ruby/builtins";
1313

1414
static void
15-
xml_xpath_context_deallocate(void *data)
15+
_noko_xml_xpath_context_dfree(void *data)
1616
{
1717
xmlXPathContextPtr c_context = data;
1818
xmlXPathFreeContext(c_context);
1919
}
2020

21-
static const rb_data_type_t xml_xpath_context_type = {
21+
static const rb_data_type_t _noko_xml_xpath_context_type = {
2222
.wrap_struct_name = "xmlXPathContext",
2323
.function = {
24-
.dfree = xml_xpath_context_deallocate,
24+
.dfree = _noko_xml_xpath_context_dfree,
2525
},
2626
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
2727
};
2828

2929
/* find a CSS class in an HTML element's `class` attribute */
3030
static const xmlChar *
31-
builtin_css_class(const xmlChar *str, const xmlChar *val)
31+
_noko_xml_xpath_context__css_class(const xmlChar *str, const xmlChar *val)
3232
{
3333
int val_len;
3434

@@ -62,9 +62,9 @@ builtin_css_class(const xmlChar *str, const xmlChar *val)
6262
return (NULL);
6363
}
6464

65-
/* xmlXPathFunction to wrap builtin_css_class() */
65+
/* xmlXPathFunction to wrap _noko_xml_xpath_context__css_class() */
6666
static void
67-
xpath_builtin_css_class(xmlXPathParserContextPtr ctxt, int nargs)
67+
noko_xml_xpath_context_xpath_func_css_class(xmlXPathParserContextPtr ctxt, int nargs)
6868
{
6969
xmlXPathObjectPtr hay, needle;
7070

@@ -85,7 +85,7 @@ xpath_builtin_css_class(xmlXPathParserContextPtr ctxt, int nargs)
8585
XP_ERROR(XPATH_INVALID_TYPE);
8686
}
8787

88-
if (builtin_css_class(hay->stringval, needle->stringval)) {
88+
if (_noko_xml_xpath_context__css_class(hay->stringval, needle->stringval)) {
8989
valuePush(ctxt, xmlXPathNewBoolean(1));
9090
} else {
9191
valuePush(ctxt, xmlXPathNewBoolean(0));
@@ -99,7 +99,7 @@ xpath_builtin_css_class(xmlXPathParserContextPtr ctxt, int nargs)
9999
/* xmlXPathFunction to select nodes whose local name matches, for HTML5 CSS queries that should
100100
* ignore namespaces */
101101
static void
102-
xpath_builtin_local_name_is(xmlXPathParserContextPtr ctxt, int nargs)
102+
noko_xml_xpath_context_xpath_func_local_name_is(xmlXPathParserContextPtr ctxt, int nargs)
103103
{
104104
xmlXPathObjectPtr element_name;
105105

@@ -129,12 +129,12 @@ xpath_builtin_local_name_is(xmlXPathParserContextPtr ctxt, int nargs)
129129
* [Returns] +self+
130130
*/
131131
static VALUE
132-
rb_xml_xpath_context_register_ns(VALUE rb_context, VALUE prefix, VALUE uri)
132+
noko_xml_xpath_context_register_ns(VALUE rb_context, VALUE prefix, VALUE uri)
133133
{
134134
xmlXPathContextPtr c_context;
135135
const xmlChar *ns_uri;
136136

137-
TypedData_Get_Struct(rb_context, xmlXPathContext, &xml_xpath_context_type, c_context);
137+
TypedData_Get_Struct(rb_context, xmlXPathContext, &_noko_xml_xpath_context_type, c_context);
138138

139139
if (NIL_P(uri)) {
140140
ns_uri = NULL;
@@ -164,12 +164,12 @@ rb_xml_xpath_context_register_ns(VALUE rb_context, VALUE prefix, VALUE uri)
164164
* [Returns] +self+
165165
*/
166166
static VALUE
167-
rb_xml_xpath_context_register_variable(VALUE rb_context, VALUE name, VALUE value)
167+
noko_xml_xpath_context_register_variable(VALUE rb_context, VALUE name, VALUE value)
168168
{
169169
xmlXPathContextPtr c_context;
170170
xmlXPathObjectPtr xmlValue;
171171

172-
TypedData_Get_Struct(rb_context, xmlXPathContext, &xml_xpath_context_type, c_context);
172+
TypedData_Get_Struct(rb_context, xmlXPathContext, &_noko_xml_xpath_context_type, c_context);
173173

174174
if (NIL_P(value)) {
175175
xmlValue = NULL;
@@ -195,7 +195,7 @@ rb_xml_xpath_context_register_variable(VALUE rb_context, VALUE name, VALUE value
195195
* returns Qundef if no conversion was possible.
196196
*/
197197
static VALUE
198-
xpath2ruby(xmlXPathObjectPtr c_xpath_object, xmlXPathContextPtr c_context)
198+
_noko_xml_xpath_context__xpath2ruby(xmlXPathObjectPtr c_xpath_object, xmlXPathContextPtr c_context)
199199
{
200200
VALUE rb_retval;
201201

@@ -249,7 +249,7 @@ Nokogiri_marshal_xpath_funcall_and_return_values(
249249

250250
for (int j = argc - 1 ; j >= 0 ; --j) {
251251
c_xpath_object = valuePop(ctxt);
252-
argv[j] = xpath2ruby(c_xpath_object, ctxt->context);
252+
argv[j] = _noko_xml_xpath_context__xpath2ruby(c_xpath_object, ctxt->context);
253253
if (argv[j] == Qundef) {
254254
argv[j] = NOKOGIRI_STR_NEW2(xmlXPathCastToString(c_xpath_object));
255255
}
@@ -305,7 +305,7 @@ Nokogiri_marshal_xpath_funcall_and_return_values(
305305
}
306306

307307
static void
308-
method_caller(xmlXPathParserContextPtr ctxt, int argc)
308+
_noko_xml_xpath_context__handler_invoker(xmlXPathParserContextPtr ctxt, int argc)
309309
{
310310
VALUE rb_xpath_handler = Qnil;
311311
const char *method_name = NULL ;
@@ -327,23 +327,23 @@ method_caller(xmlXPathParserContextPtr ctxt, int argc)
327327
}
328328

329329
static xmlXPathFunction
330-
handler_lookup(void *data, const xmlChar *c_name, const xmlChar *c_ns_uri)
330+
_noko_xml_xpath_context_handler_lookup(void *data, const xmlChar *c_name, const xmlChar *c_ns_uri)
331331
{
332332
VALUE rb_handler = (VALUE)data;
333333
if (rb_respond_to(rb_handler, rb_intern((const char *)c_name))) {
334334
if (c_ns_uri == NULL) {
335335
NOKO_WARN_DEPRECATION("A custom XPath or CSS handler function named '%s' is being invoked without a namespace. Please update your query to reference this function as 'nokogiri:%s'. Invoking custom handler functions without a namespace is deprecated and will become an error in Nokogiri v1.17.0.",
336-
c_name, c_name); // deprecated in v1.15.0, remove in v1.17.0
336+
c_name, c_name); // TODO deprecated in v1.15.0, remove in v1.19.0
337337
}
338-
return method_caller;
338+
return _noko_xml_xpath_context__handler_invoker;
339339
}
340340

341341
return NULL;
342342
}
343343

344344
PRINTFLIKE_DECL(2, 3)
345345
static void
346-
generic_exception_pusher(void *data, const char *msg, ...)
346+
_noko_xml_xpath_context__generic_exception_pusher(void *data, const char *msg, ...)
347347
{
348348
VALUE rb_errors = (VALUE)data;
349349
VALUE rb_message;
@@ -354,7 +354,7 @@ generic_exception_pusher(void *data, const char *msg, ...)
354354
#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
355355
/* It is not currently possible to pass var args from native
356356
functions to sulong, so we work around the issue here. */
357-
rb_message = rb_sprintf("generic_exception_pusher: %s", msg);
357+
rb_message = rb_sprintf("_noko_xml_xpath_context__generic_exception_pusher: %s", msg);
358358
#else
359359
va_list args;
360360
va_start(args, msg);
@@ -376,59 +376,54 @@ generic_exception_pusher(void *data, const char *msg, ...)
376376
* a +Float+, or a boolean.
377377
*/
378378
static VALUE
379-
rb_xml_xpath_context_evaluate(int argc, VALUE *argv, VALUE rb_context)
379+
noko_xml_xpath_context_evaluate(int argc, VALUE *argv, VALUE rb_context)
380380
{
381-
VALUE search_path, xpath_handler;
382-
VALUE retval = Qnil;
383381
xmlXPathContextPtr c_context;
384-
xmlXPathObjectPtr xpath;
385-
xmlChar *query;
386-
VALUE errors = rb_ary_new();
387-
388-
TypedData_Get_Struct(
389-
rb_context,
390-
xmlXPathContext,
391-
&xml_xpath_context_type,
392-
c_context
393-
);
382+
VALUE rb_expression = Qnil;
383+
VALUE rb_function_lookup_handler = Qnil;
384+
xmlChar *c_expression_str = NULL;
385+
VALUE rb_errors = rb_ary_new();
386+
xmlXPathObjectPtr c_xpath_object;
387+
VALUE rb_xpath_object = Qnil;
394388

395-
if (rb_scan_args(argc, argv, "11", &search_path, &xpath_handler) == 1) {
396-
xpath_handler = Qnil;
397-
}
389+
TypedData_Get_Struct(rb_context, xmlXPathContext, &_noko_xml_xpath_context_type, c_context);
398390

399-
query = (xmlChar *)StringValueCStr(search_path);
391+
rb_scan_args(argc, argv, "11", &rb_expression, &rb_function_lookup_handler);
400392

401-
if (Qnil != xpath_handler) {
393+
c_expression_str = (xmlChar *)StringValueCStr(rb_expression);
394+
395+
if (Qnil != rb_function_lookup_handler) {
402396
/* FIXME: not sure if this is the correct place to shove private data. */
403-
c_context->userData = (void *)xpath_handler;
397+
c_context->userData = (void *)rb_function_lookup_handler;
404398
xmlXPathRegisterFuncLookup(
405399
c_context,
406-
handler_lookup,
407-
(void *)xpath_handler
400+
_noko_xml_xpath_context_handler_lookup,
401+
(void *)rb_function_lookup_handler
408402
);
409403
}
410404

411-
xmlSetStructuredErrorFunc((void *)errors, noko__error_array_pusher);
412-
xmlSetGenericErrorFunc((void *)errors, generic_exception_pusher);
405+
xmlSetStructuredErrorFunc((void *)rb_errors, noko__error_array_pusher);
406+
xmlSetGenericErrorFunc((void *)rb_errors, _noko_xml_xpath_context__generic_exception_pusher);
413407

414-
xpath = xmlXPathEvalExpression(query, c_context);
408+
c_xpath_object = xmlXPathEvalExpression(c_expression_str, c_context);
415409

416410
xmlSetStructuredErrorFunc(NULL, NULL);
417411
xmlSetGenericErrorFunc(NULL, NULL);
412+
418413
xmlXPathRegisterFuncLookup(c_context, NULL, NULL);
419414

420-
if (xpath == NULL) {
421-
rb_exc_raise(rb_ary_entry(errors, 0));
415+
if (c_xpath_object == NULL) {
416+
rb_exc_raise(rb_ary_entry(rb_errors, 0));
422417
}
423418

424-
retval = xpath2ruby(xpath, c_context);
425-
if (retval == Qundef) {
426-
retval = noko_xml_node_set_wrap(NULL, DOC_RUBY_OBJECT(c_context->doc));
419+
rb_xpath_object = _noko_xml_xpath_context__xpath2ruby(c_xpath_object, c_context);
420+
if (rb_xpath_object == Qundef) {
421+
rb_xpath_object = noko_xml_node_set_wrap(NULL, DOC_RUBY_OBJECT(c_context->doc));
427422
}
428423

429-
xmlXPathFreeNodeSetList(xpath);
424+
xmlXPathFreeNodeSetList(c_xpath_object);
430425

431-
return retval;
426+
return rb_xpath_object;
432427
}
433428

434429
/*
@@ -438,7 +433,7 @@ rb_xml_xpath_context_evaluate(int argc, VALUE *argv, VALUE rb_context)
438433
* Create a new XPathContext with +node+ as the context node.
439434
*/
440435
static VALUE
441-
rb_xml_xpath_context_new(VALUE klass, VALUE rb_node)
436+
noko_xml_xpath_context_new(VALUE klass, VALUE rb_node)
442437
{
443438
xmlNodePtr c_node;
444439
xmlXPathContextPtr c_context;
@@ -447,8 +442,7 @@ rb_xml_xpath_context_new(VALUE klass, VALUE rb_node)
447442
Noko_Node_Get_Struct(rb_node, xmlNode, c_node);
448443

449444
#if LIBXML_VERSION < 21000
450-
/* deprecated in 40483d0 */
451-
xmlXPathInit();
445+
xmlXPathInit(); /* deprecated in 40483d0 */
452446
#endif
453447

454448
c_context = xmlXPathNewContext(c_node->doc);
@@ -459,16 +453,12 @@ rb_xml_xpath_context_new(VALUE klass, VALUE rb_node)
459453

460454
xmlXPathRegisterFuncNS(c_context,
461455
(const xmlChar *)"css-class", NOKOGIRI_BUILTIN_URI,
462-
xpath_builtin_css_class);
456+
noko_xml_xpath_context_xpath_func_css_class);
463457
xmlXPathRegisterFuncNS(c_context,
464458
(const xmlChar *)"local-name-is", NOKOGIRI_BUILTIN_URI,
465-
xpath_builtin_local_name_is);
459+
noko_xml_xpath_context_xpath_func_local_name_is);
466460

467-
rb_context = TypedData_Wrap_Struct(
468-
klass,
469-
&xml_xpath_context_type,
470-
c_context
471-
);
461+
rb_context = TypedData_Wrap_Struct(klass, &_noko_xml_xpath_context_type, c_context);
472462

473463
rb_iv_set(rb_context, "@registered_namespaces", rb_hash_new());
474464
rb_iv_set(rb_context, "@registered_variables", rb_hash_new());
@@ -479,12 +469,12 @@ rb_xml_xpath_context_new(VALUE klass, VALUE rb_node)
479469

480470
/* :nodoc: */
481471
static VALUE
482-
rb_xml_xpath_context_set_node(VALUE rb_context, VALUE rb_node)
472+
noko_xml_xpath_context_set_node(VALUE rb_context, VALUE rb_node)
483473
{
484474
xmlNodePtr c_node;
485475
xmlXPathContextPtr c_context;
486476

487-
TypedData_Get_Struct(rb_context, xmlXPathContext, &xml_xpath_context_type, c_context);
477+
TypedData_Get_Struct(rb_context, xmlXPathContext, &_noko_xml_xpath_context_type, c_context);
488478
Noko_Node_Get_Struct(rb_node, xmlNode, c_node);
489479

490480
c_context->doc = c_node->doc;
@@ -503,10 +493,10 @@ noko_init_xml_xpath_context(void)
503493

504494
rb_undef_alloc_func(cNokogiriXmlXpathContext);
505495

506-
rb_define_singleton_method(cNokogiriXmlXpathContext, "new", rb_xml_xpath_context_new, 1);
496+
rb_define_singleton_method(cNokogiriXmlXpathContext, "new", noko_xml_xpath_context_new, 1);
507497

508-
rb_define_method(cNokogiriXmlXpathContext, "evaluate", rb_xml_xpath_context_evaluate, -1);
509-
rb_define_method(cNokogiriXmlXpathContext, "register_variable", rb_xml_xpath_context_register_variable, 2);
510-
rb_define_method(cNokogiriXmlXpathContext, "register_ns", rb_xml_xpath_context_register_ns, 2);
511-
rb_define_method(cNokogiriXmlXpathContext, "node=", rb_xml_xpath_context_set_node, 1);
498+
rb_define_method(cNokogiriXmlXpathContext, "evaluate", noko_xml_xpath_context_evaluate, -1);
499+
rb_define_method(cNokogiriXmlXpathContext, "register_variable", noko_xml_xpath_context_register_variable, 2);
500+
rb_define_method(cNokogiriXmlXpathContext, "register_ns", noko_xml_xpath_context_register_ns, 2);
501+
rb_define_method(cNokogiriXmlXpathContext, "node=", noko_xml_xpath_context_set_node, 1);
512502
}

0 commit comments

Comments
 (0)