Re: [xml] XPath normalize-string

Date view Thread view Subject view Author view

From: Bjorn Reese (breese@mail1.stofanet.dk)
Date: Sun Sep 24 2000 - 12:44:15 EDT


While I was at it, I also implemented the substring-before, substring-after,
and translate functions.

The translate implementation is fairly naive, and probably doesn't work for
all cases of UTF-8 strings, but a naive implementation is still better than
none.

Regarding testing, I was unable to use testXPath to test translate because
it disliked spaces in literal strings, so I wrote a small testing program
of my own. I have not tested border-line cases though.

*** libxml2-2.2.3/xpath.c Sat Sep 16 21:46:39 2000
--- libxml/xpath.c Sun Sep 24 18:28:13 2000
***************
*** 2760,2767 ****
   */
  void
  xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
! CHECK_ARITY(2);
! TODO /* substring before */
  }
  
  /**
--- 2760,2788 ----
   */
  void
  xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
! xmlXPathObjectPtr str;
! xmlXPathObjectPtr find;
! xmlBufferPtr target;
! const xmlChar *point;
! int offset;
!
! CHECK_ARITY(2);
! find = valuePop(ctxt);
! str = valuePop(ctxt);
!
! target = xmlBufferCreate();
! if (target) {
! point = xmlStrstr(str->stringval, find->stringval);
! if (point) {
! offset = (int)(point - str->stringval);
! xmlBufferAdd(target, str->stringval, offset);
! }
! valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
! xmlBufferFree(target);
! }
!
! xmlXPathFreeObject(str);
! xmlXPathFreeObject(find);
  }
  
  /**
***************
*** 2778,2785 ****
   */
  void
  xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) {
! CHECK_ARITY(2);
! TODO /* substring after */
  }
  
  /**
--- 2799,2828 ----
   */
  void
  xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) {
! xmlXPathObjectPtr str;
! xmlXPathObjectPtr find;
! xmlBufferPtr target;
! const xmlChar *point;
! int offset;
!
! CHECK_ARITY(2);
! find = valuePop(ctxt);
! str = valuePop(ctxt);
!
! target = xmlBufferCreate();
! if (target) {
! point = xmlStrstr(str->stringval, find->stringval);
! if (point) {
! offset = (int)(point - str->stringval) + xmlStrlen(find->stringval);
! xmlBufferAdd(target, &str->stringval[offset],
! xmlStrlen(str->stringval) - offset);
! }
! valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
! xmlBufferFree(target);
! }
!
! xmlXPathFreeObject(str);
! xmlXPathFreeObject(find);
  }
  
  /**
***************
*** 2821,2828 ****
   */
  void
  xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) {
! CHECK_ARITY(3);
! TODO /* translate is boring, waiting for UTF-8 representation too */
  }
  
  /**
--- 2905,2943 ----
   */
  void
  xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) {
! xmlXPathObjectPtr str;
! xmlXPathObjectPtr from;
! xmlXPathObjectPtr to;
! xmlBufferPtr target;
! int i, offset, max;
! xmlChar ch;
! const xmlChar *point;
!
! CHECK_ARITY(3);
!
! to = valuePop(ctxt);
! from = valuePop(ctxt);
! str = valuePop(ctxt);
!
! target = xmlBufferCreate();
! if (target) {
! max = xmlStrlen(to->stringval);
! for (i = 0; (ch = str->stringval[i]); i++) {
! point = xmlStrchr(from->stringval, ch);
! if (point) {
! /* Warning: This may not work with UTF-8 */
! offset = (int)(point - from->stringval);
! if (offset < max)
! xmlBufferAdd(target, &to->stringval[offset], 1);
! } else
! xmlBufferAdd(target, &ch, 1);
! }
! }
! valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
! xmlBufferFree(target);
! xmlXPathFreeObject(str);
! xmlXPathFreeObject(from);
! xmlXPathFreeObject(to);
  }
  
  /**

----
Message from the list xml@rpmfind.net
Archived at : http://xmlsoft.org/messages/
to unsubscribe: echo "unsubscribe xml" | mail  majordomo@rpmfind.net


Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Sun Sep 24 2000 - 12:43:14 EDT