Example #1
0
static SLang_BString_Type *create_bstring_of_type (char *bytes, unsigned int len, int type)
{
   SLang_BString_Type *b;
   unsigned int size;
   unsigned int malloced_len = len;

   size = sizeof(SLang_BString_Type);
   if (type == IS_BSTRING)
     {
	unsigned int dlen = BSTRING_EXTRA_BYTES(len);
	malloced_len = len + dlen;
	if ((malloced_len < len)
	    || (size + malloced_len < size))
	  {
	     SLang_verror (SL_Malloc_Error, "Unable to create a binary string of the desired size");
	     return NULL;
	  }
	size += malloced_len;
     }

   if (NULL == (b = (SLang_BString_Type *)SLmalloc (size)))
     return NULL;

   b->len = len;
   b->malloced_len = malloced_len;
   b->num_refs = 1;
   b->ptr_type = type;

   switch (type)
     {
      default:
      case IS_BSTRING:
	if (bytes != NULL) memcpy ((char *) b->v.bytes, bytes, len);
	/* Now \0 terminate it because we want to also use it as a C string
	 * whenever possible.  Note that sizeof(SLang_BString_Type) includes
	 * space for 1 character and we allocated len extra bytes.  Thus, it is
	 * ok to add a \0 to the end.
	 */
	b->v.bytes[len] = 0;
	break;

      case IS_SLSTRING:
	if (NULL == (b->v.ptr = (unsigned char *)SLang_create_nslstring (bytes, len)))
	  {
	     SLfree ((char *) b);
	     return NULL;
	  }
	break;

      case IS_MALLOCED:
      case IS_NOT_TO_BE_FREED:
	b->v.ptr = (unsigned char *)bytes;
	bytes [len] = 0;	       /* NULL terminate */
	break;
     }

   return b;
}
Example #2
0
static int parse_range (SLFUTURE_CONST char **sp, SLFUTURE_CONST char *smax, SLFUTURE_CONST char **fp, char **str)
{
   SLFUTURE_CONST char *s, *s0;
   char *range;
   SLFUTURE_CONST char *f;
   unsigned char map[256];
   unsigned char reverse;

   /* How can one represent a range with just '^'?  The naive answer is
    * is [^].  However, this may be interpreted as meaning any character
    * but ']' and others.  Let's assume that the user will not use a range
    * to match '^'.
    */
   f = *fp;
   /* f is a pointer to (one char after) [...]. */
   if (*f == '^')
     {
	f++;
	reverse = 1;
     }
   else reverse = 0;

   s0 = f;
   if (*f == ']')
     f++;

   while (1)
     {
	char ch = *f;

	if (ch == 0)
	  {
	     _pSLang_verror (SL_INVALID_PARM, "Unexpected end of range in format");
	     return -1;
	  }
	if (ch == ']')
	  break;
	f++;
     }
   if (NULL == (range = SLmake_nstring (s0, (unsigned int) (f - s0))))
     return -1;
   *fp = f + 1;			       /* skip ] */
   
   SLmake_lut (map, (unsigned char *) range, reverse);
   SLfree (range);

   s0 = s = *sp;
   while ((s < smax) && map [(unsigned char) *s])
     s++;
   
   if (NULL == (*str = SLang_create_nslstring (s0, (unsigned int) (s - s0))))
     return -1;
   
   *sp = s;
   return 1;
}
Example #3
0
static int parse_bstring (SLFUTURE_CONST char **sp, SLFUTURE_CONST char *smax, char **str)
{
   SLFUTURE_CONST char *s;
   
   s = *sp;
   if (NULL == (*str = SLang_create_nslstring (s, (unsigned int) (smax - s))))
     return -1;
   
   *sp = smax;
   return 1;
}
Example #4
0
static int parse_string (SLFUTURE_CONST char **sp, SLFUTURE_CONST char *smax, char **str)
{
   SLFUTURE_CONST char *s, *s0;
   
   s0 = s = *sp;
   while (s < smax)
     {
	if (isspace (*s))
	  break;
	s++;
     }
   if (NULL == (*str = SLang_create_nslstring (s0, (unsigned int) (s - s0))))
     return -1;
   
   *sp = s;
   return 1;
}
Example #5
0
static SLang_BString_Type *create_bstring_of_type (char *bytes, unsigned int len, int type)
{
   SLang_BString_Type *b;
   unsigned int size;

   size = sizeof(SLang_BString_Type);
   if (type == 0)
     size += len;

   if (NULL == (b = (SLang_BString_Type *)SLmalloc (size)))
     return NULL;

   b->len = len;
   b->num_refs = 1;
   b->ptr_type = type;

   switch (type)
     {
      case 0:
	if (bytes != NULL) memcpy ((char *) b->v.bytes, bytes, len);
	/* Now \0 terminate it because we want to also use it as a C string
	 * whenever possible.  Note that sizeof(SLang_BString_Type) includes
	 * space for 1 character and we allocated len extra bytes.  Thus, it is
	 * ok to add a \0 to the end.
	 */
	b->v.bytes[len] = 0;
	break;

      case IS_SLSTRING:
	if (NULL == (b->v.ptr = (unsigned char *)SLang_create_nslstring (bytes, len)))
	  {
	     SLfree ((char *) b);
	     return NULL;
	  }
	break;

      case IS_MALLOCED:
      case IS_NOT_TO_BE_FREED:
	b->v.ptr = (unsigned char *)bytes;
	bytes [len] = 0;	       /* NULL terminate */
	break;
     }

   return b;
}
Example #6
0
static void nth_substr (Onig_Type *o, char *str, int *np)
{
   unsigned int start, stop;
   unsigned int len;

   len = strlen (str);

   if ((-1 == get_nth_start_stop (o, (unsigned int) *np, &start, &stop))
       || (start > len) || (stop > len))
     {
	SLang_push_null ();
	return;
     }

   str = SLang_create_nslstring (str + start, stop - start);
   (void) SLang_push_string (str);
   SLang_free_slstring (str);
}
Example #7
0
static SLuchar_Type *xform_utf8 (SLuchar_Type *u, SLuchar_Type *umax,
                                 SLwchar_Type (*fun)(SLwchar_Type))
{
   SLuchar_Type *buf, *p;
   unsigned int malloced_len, len;

   if (umax < u)
     return NULL;
   
   len = 0;
   p = buf = NULL;
   malloced_len = 0;

   while (1)
     {
        SLwchar_Type w;
        SLuchar_Type *u1;
        unsigned int nconsumed;

        if (malloced_len <= len + SLUTF8_MAX_MBLEN)
          {
             SLuchar_Type *newbuf;
             malloced_len += 1 + (umax - u) + SLUTF8_MAX_MBLEN;
             
             newbuf = (SLuchar_Type *)SLrealloc ((char *)buf, malloced_len);
             if (newbuf == NULL)
               {
                  SLfree ((char *)buf);
                  return NULL;
               }
             buf = newbuf;
             p = buf + len;
          }

        if (u >= umax)
          {
             *p = 0;
             p = (SLuchar_Type *) SLang_create_nslstring ((char *)buf, len);
             SLfree ((char *)buf);
             return p;
          }

        if (NULL == (u1 = SLutf8_decode (u, umax, &w, &nconsumed)))
          {
             /* Invalid sequence */
             memcpy ((char *) p, u, nconsumed);
             p += nconsumed;
             len += nconsumed;
             u1 = u + nconsumed;
          }
        else
          {
             SLuchar_Type *p1;

             p1 = SLutf8_encode ((*fun)(w), p, malloced_len);
             if (p1 == NULL)
               {
                  SLfree ((char *)buf);
                  _pSLang_verror (SL_INTERNAL_ERROR, "SLutf8_encode returned NULL");
                  return NULL;
               }
             len += p1 - p;
             p = p1;
          }
        
        u = u1;
     }
}