RML_END_LABEL

/* list_reverse.c */
RML_BEGIN_LABEL(RML__list_5freverse)
{
    void *a1;		/* cached A1 */
    struct rml_struct *cons;

    /* A1 := A0; A0 := NIL */
    a1 = rmlA0;
    rmlA0 = RML_TAGPTR(&rml_prim_nil);

    /* while CONSP(A1) do A0 := CONS(CAR(A1), A0); A1 := CDR(A1) end */
    while( RML_GETHDR(a1) == RML_CONSHDR ) {
	rmlA1 = a1;
	cons = (struct rml_struct*)rml_prim_alloc(3, 2);
	a1 = rmlA1;
	cons->header = RML_CONSHDR;
	cons->data[0] = RML_CAR(a1);
	cons->data[1] = rmlA0;
	rmlA0 = RML_TAGPTR(cons);
	a1 = RML_CDR(a1);
    }

    /* return A0 */
    RML_TAILCALLK(rmlSC);
}
Exemple #2
0
RML_END_LABEL


RML_BEGIN_LABEL(External__substring)
{
	rml_uint_t len = RML_HDRSTRLEN(RML_GETHDR(rmlA0)); /* string lenght */
	int index1 = RML_UNTAGFIXNUM(rmlA1);
	int index2 = RML_UNTAGFIXNUM(rmlA2);
	rml_uint_t newlen = 0;
	int i = 0;
	if (index2 < 0) index2 = len-1;
	if (index1 < 0) index1 = 0;
	if (index1 > index2)
	{
		index1 = RML_UNTAGFIXNUM(rmlA2);
		index2 = RML_UNTAGFIXNUM(rmlA1);
	}
	if (index2 >= len) index2 = len-1;
	newlen = index2-index1 + 1;
	/* alloc the new string */
	struct rml_string *strnew = rml_prim_mkstring(newlen, 3);
	char *str = RML_STRINGDATA(rmlA0);
	unsigned char *snew = (unsigned char*)strnew->data;
	for(i=index1; i <= index2; i++)
	{
		*snew++ = str[i];
	}
	*snew = '\0';
	rmlA0 = RML_TAGPTR(strnew);
	RML_TAILCALLK(rmlSC);
}
RML_END_LABEL


/* list-arr.c */
RML_BEGIN_LABEL(RML__list_5farray)
{
    rml_uint_t nelts = 0;

    /* first compute the length of the list */
    {
	void *lst = rmlA0;
	for(; RML_GETHDR(lst) == RML_CONSHDR; ++nelts, lst = RML_CDR(lst))
	    ;
    }
    /* then allocate and initialize the vector */
    {
	struct rml_struct *vec = (struct rml_struct*)rml_prim_alloc(1+nelts, 1);
	void *lst = rmlA0;
	void **vecp = vec->data;
	vec->header = RML_STRUCTHDR(nelts, 0);
	rmlA0 = RML_TAGPTR(vec);
	for(; nelts > 0; --nelts, lst = RML_CDR(lst))
	    *vecp++ = RML_CAR(lst);
    }
    RML_TAILCALLK(rmlSC);
}
Exemple #4
0
RML_END_LABEL


RML_BEGIN_LABEL(External__getFirstIdent)
{
	rml_uint_t len = RML_HDRSTRLEN(RML_GETHDR(rmlA0)); /* string lenght */
	rml_uint_t newlen = 0;
	int index = 1, i=0;
	char *str = RML_STRINGDATA(rmlA0);
	if (!isalpha(str[0])) RML_TAILCALLK(rmlFC); /* fail if we don't start with alpha */
	while((isalpha(str[index]) ||
		  (str[index] >= '0' && str[index] <= '9')) &&
		  index < len) index++;
	/* alloc the new string */
	struct rml_string *strnew = rml_prim_mkstring(index, 3);
	str = RML_STRINGDATA(rmlA0);
	unsigned char *snew = (unsigned char*)strnew->data;
	for(i=0; i < index; i++)
	{
		*snew++ = str[i];
	}
	*snew = '\0';
	rmlA0 = RML_TAGPTR(strnew);
	RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

/* adrpo added string char list to string */
RML_BEGIN_LABEL(RML__string_5fchar_5flist_5fstring)
{
    rml_uint_t len = 0;

    /* first compute the length of the list */
    {
	void *lst = rmlA0;
	for(; RML_GETHDR(lst) == RML_CONSHDR; ++len, lst = RML_CDR(lst))
	    ;
    }
    /* then allocate and initialize the string */
    {
	struct rml_string *str = rml_prim_mkstring(len, 1);	/* gets len+1 bytes */
	void *lst = rmlA0;
	unsigned char *s = (unsigned char*)str->data;
	rmlA0 = RML_TAGPTR(str);
	for(; len > 0; --len, lst = RML_CDR(lst))
	{
		/* printf ("%c ",RML_STRINGDATA(RML_CAR(lst))[0]) */
	    *s++ = RML_STRINGDATA(RML_CAR(lst))[0];
	}
	*s = '\0';
    }
    RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

/* list-str.c */
RML_BEGIN_LABEL(RML__list_5fstring)
{
    rml_uint_t len = 0;

    /* first compute the length of the list */
    {
	void *lst = rmlA0;
	for(; RML_GETHDR(lst) == RML_CONSHDR; ++len, lst = RML_CDR(lst))
	    ;
    }
    /* then allocate and initialize the string */
    {
	struct rml_string *str = rml_prim_mkstring(len, 1);	/* gets len+1 bytes */
	void *lst = rmlA0;
	unsigned char *s = (unsigned char*)str->data;
	rmlA0 = RML_TAGPTR(str);
	for(; len > 0; --len, lst = RML_CDR(lst))
	    *s++ = RML_UNTAGFIXNUM(RML_CAR(lst));
	*s = '\0';
    }
    RML_TAILCALLK(rmlSC);
}
Exemple #7
0
void *mk_box1(unsigned ctor, void *x0)
{
    struct rml_struct *p = alloc_words(2);
    p->header = RML_STRUCTHDR(1, ctor);
    p->data[0] = x0;
    return RML_TAGPTR(p);
}
void *mk_box2(unsigned ctor, void *x0, void *x1) {
  struct rml_struct *p = alloc_words(3);
  p->header = RML_STRUCTHDR(2, ctor);
  p->data[0] = x0;
  p->data[1] = x1;
  return RML_TAGPTR(p);
}
Exemple #9
0
void *mk_rcon(double d)
{
    struct rml_real *p = alloc_words(RML_SIZE_DBL/RML_SIZE_INT + 1);
    rml_prim_set_real(p, d);
    p->header = RML_REALHDR;
    return RML_TAGPTR(p);
}
void *mk_box3(unsigned ctor, void *x0, void *x1, void *x2) {
  struct rml_struct *p = alloc_words(4);
  p->header = RML_STRUCTHDR(3, ctor);
  p->data[0] = x0;
  p->data[1] = x1;
  p->data[2] = x2;
  return RML_TAGPTR(p);
}
void *mk_box4(unsigned ctor, void *x0, void *x1, void *x2, void *x3) {
  struct rml_struct *p = alloc_words(5);
  p->header = RML_STRUCTHDR(4, ctor);
  p->data[0] = x0;
  p->data[1] = x1;
  p->data[2] = x2;
  p->data[3] = x3;
  return RML_TAGPTR(p);
}
Exemple #12
0
RML_END_LABEL


RML_BEGIN_LABEL(External__strrpl)
{
	rml_uint_t len1 = RML_HDRSTRLEN(RML_GETHDR(rmlA0)); /* string lenght */
	rml_uint_t len2 = RML_HDRSTRLEN(RML_GETHDR(rmlA1)); /* string lenght */
	rml_uint_t len3 = RML_HDRSTRLEN(RML_GETHDR(rmlA2)); /* string lenght */
	char *str1 = RML_STRINGDATA(rmlA0);
	char *str2 = RML_STRINGDATA(rmlA1);
	char *str3 = RML_STRINGDATA(rmlA2);
	char *strpos;
	if (len1 == 0 || len2==0)
	{
		rmlA0 = rmlA0; /* return the first string unchanged */
		RML_TAILCALLK(rmlSC);
	}
	if ((strpos = strstr(str1, str2)) == NULL) /* the string is not there */
	{
		rmlA0 = rmlA0; /* return the first string unchanged */
		RML_TAILCALLK(rmlSC);
	}
	else
	{
		/* string is there */
		rml_uint_t len = len1-len2+len3;
		/* find where */
		rml_uint_t pos = (int)(strpos - str1);
		/* alloc the new string */
		struct rml_string *strnew = rml_prim_mkstring(len, 3);
		int i, j, k;
		/* reread the rmlAX, it could have been moved by the GC */
		str1 = RML_STRINGDATA(rmlA0);
		str2 = RML_STRINGDATA(rmlA1);
		str3 = RML_STRINGDATA(rmlA2);
		unsigned char *snew = (unsigned char*)strnew->data;
		/* until pos, use the first string */
		for(i=0; i < pos; i++)
		{
			*snew++ = str1[i];
		}
		/* now use str3 */
		for(i=0; i < len3; i++)
		{
			*snew++ = str3[i];
		}
		/* until end, use the first string again */
		for(i=pos+len2; i < len1; i++)
		{
			*snew++ = str1[i];
		}
		*snew = '\0';
		rmlA0 = RML_TAGPTR(strnew);
		RML_TAILCALLK(rmlSC);
	}
}
Exemple #13
0
void *mk_scon(char *s)
{
    unsigned nbytes = strlen(s);
    unsigned header = RML_STRINGHDR(nbytes);
    unsigned nwords = RML_HDRSLOTS(header) + 1;
    struct rml_string *p = alloc_words(nwords);
    p->header = header;
    memcpy(p->data, s, nbytes+1);	/* including terminating '\0' */
    return RML_TAGPTR(p);
}
Exemple #14
0
RML_END_LABEL


RML_BEGIN_LABEL(External__toJavaName)
{
    void *a0 = rmlA0;
    char *str = RML_STRINGDATA(a0);
	int i = 0;
	int j = 1;
	rml_uint_t len = RML_HDRSTRLEN(RML_GETHDR(rmlA0)); /* string lenght */
	if (len < 1) RML_TAILCALLK(rmlSC);
    /* check if are all caps or "_"
	 * if they are, do nothing!
	 */
	for (; i < len;)
    if (str[i] != '_' && str[i] != toupper(str[i]))
		break;
	else i++;
	if (i==len) RML_TAILCALLK(rmlSC); /* all caps or "_"; return the same */
	i = 1;
	char *newstr = (char*)malloc(len+1);
	newstr[0] = tolower(str[0]); /* make the first one lowercase */
	char *freeme = newstr;
	for (; i < len;)
	if (str[i] != '_')
	{
		newstr[j++] = str[i];
		i++;
	}
	else /* is equal */
	{
       if (i+1 < len)
	   {
		    newstr[j++]=toupper(str[i+1]);
			i += 2;
	   }
	   else
	   {
			newstr[j++] = str[i];
			i++;
	   }
	}
	newstr[j] = '\0';
	len = strlen(newstr);
	/* alloc the new string */
	struct rml_string *strnew = rml_prim_mkstring(len, 1);
	unsigned char *snew = (unsigned char*)strnew->data;
	for(; len > 0; --len)
		*snew++ = *newstr++;
	*snew = '\0';
	rmlA0 = RML_TAGPTR(strnew);
	free(freeme);
	RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

/* lvar_new.c */
RML_BEGIN_LABEL(RML__lvar_5fnew)
{
    struct rml_ref *lvar = (struct rml_ref*)rml_prim_alloc(2, 0);
    lvar->header = RML_UNBOUNDHDR;
    /* lvar->data need not be initialized */
    rmlA0 = RML_TAGPTR(lvar);
    RML_TAILCALLK(rmlSC);
}
Exemple #16
0
RML_END_LABEL


RML_BEGIN_LABEL(External__sst)
{
	char *s;
	char *str = strdup(RML_STRINGDATA(rmlA0));
	char *delimit = RML_STRINGDATA(rmlA1);
	char *separator = RML_STRINGDATA(rmlA2);
	char *strTmp1 = 0;
	char *strTmp2 = 0;
	int i;
	rml_uint_t lenseparator = strlen(separator);
	rml_uint_t len1 = 0;
	rml_uint_t len2 = 0;
	rml_uint_t len = 0;

	void * res = (void*)mk_nil();
	s=strtok(str,delimit);
	if (s == NULL)
	{
			/* adrpo added 2004-10-27 */
			free(str);
			rmlA0=res; RML_TAILCALLK(rmlFC);
	}
	strTmp1 = strdup(s); /* first token */
	while (s=strtok(NULL,delimit))
	{
		len1 = strlen(strTmp1);
		len2 = strlen(s);
		len = len1+lenseparator+len2;
		strTmp2 = (char*)malloc(len+1);
		strTmp2[0] = '\0';
		strncat(strTmp2,strTmp1,len1);
		strncat(strTmp2,separator,lenseparator);
		strncat(strTmp2,s,len2);
		strTmp2[len] = '\0';
		free(strTmp1);
		strTmp1 = strTmp2;
	}
	/* now we have everything in strTmp2 */
	/* now alloc the new string */
	struct rml_string *strnew = rml_prim_mkstring(len, 3);
	unsigned char *snew = (unsigned char*)strnew->data;
	for(i=0; i < len; i++)
	{
		*snew++ = strTmp2[i];
	}
	*snew = '\0';
	free(strTmp2);
	rmlA0 = RML_TAGPTR(strnew);
	RML_TAILCALLK(rmlSC);
}
void *mk_box6(unsigned ctor, void *x0, void *x1, void *x2, void *x3, void *x4,
    void *x5) {
  struct rml_struct *p = alloc_words(7);
  p->header = RML_STRUCTHDR(6, ctor);
  p->data[0] = x0;
  p->data[1] = x1;
  p->data[2] = x2;
  p->data[3] = x3;
  p->data[4] = x4;
  p->data[5] = x5;
  return RML_TAGPTR(p);
}
Exemple #18
0
RML_END_LABEL


/* list-delete.c */
RML_BEGIN_LABEL(RML__list_5fdelete)
{
    rml_sint_t nelts = RML_UNTAGFIXNUM(rmlA1);
    if( nelts < 0 )
	RML_TAILCALLK(rmlFC);
    else if( nelts == 0 ) {
	if( RML_GETHDR(rmlA0) == RML_CONSHDR )
	    rmlA0 = RML_CDR(rmlA0);
	else
	    RML_TAILCALLK(rmlFC);
    } else { /* nelts > 0 */
	void **chunk = (void**)rml_prim_alloc(3*nelts, 1);
	void *lst = rmlA0;
	rmlA0 = RML_TAGPTR(chunk);
	for(;;) {
	    if( RML_GETHDR(lst) == RML_CONSHDR ) {
		if( nelts == 0 ) {
		    chunk[-1] = RML_CDR(lst);
		    break;
		} else {
		    chunk[0] = RML_IMMEDIATE(RML_CONSHDR);
		    chunk[1] = RML_CAR(lst);
		    chunk[2] = RML_TAGPTR(chunk + 3);
		    lst = RML_CDR(lst);
		    chunk += 3;
		    --nelts;
		    continue;
		}
	    } else	/* NIL */
		RML_TAILCALLK(rmlFC);
	}
    }

    /* return resulting list */
    RML_TAILCALLK(rmlSC);
}
void *mk_scon(char *s) {
  rml_uint_t nbytes = strlen(s);
  rml_uint_t header= RML_STRINGHDR(nbytes);
  rml_uint_t nwords= RML_HDRSLOTS(header) + 1;
  if (!rml_string_cache_index) /* no string in the cache */
  {
    struct rml_string *p = alloc_words(nwords);
    p->header = header;
    memcpy(p->data, s, nbytes+1); /* including terminating '\0' */
    if (rml_string_cache_index < RML_STRING_CACHE_MAX &&
        nbytes < RML_SHARED_STRING_MAX) /* add to sharing only if less than RML_SHARED_STRING_MAX */
      rml_string_cache[rml_string_cache_index++] = p;
    return RML_TAGPTR(p);
  }
  /* else, try to find if we already have the same string in the heap */
  {
    unsigned int i;
    struct rml_string *p;
    for (i = 0; i < rml_string_cache_index; i++)
    {
      p = rml_string_cache[i];
      if (strcmp(p->data,s) == 0)
      {
        rml_total_shared_strings++;
        rml_total_shared_strings_words += nwords;
        return RML_TAGPTR(p);
      }
    }
    /* no string found in cache */
    {
      struct rml_string *p = alloc_words(nwords);
      p->header = header;
      memcpy(p->data, s, nbytes+1); /* including terminating '\0' */
      if (rml_string_cache_index < RML_STRING_CACHE_MAX &&
          nbytes < RML_SHARED_STRING_MAX) /* add to sharing only if less than RML_SHARED_STRING_MAX */
        rml_string_cache[rml_string_cache_index++] = p;
      return RML_TAGPTR(p);
    }
  }
}
Exemple #20
0
RML_END_LABEL


RML_BEGIN_LABEL(External__trimstring)
{
	char *str = RML_STRINGDATA(rmlA0);
	char *what = RML_STRINGDATA(rmlA1);
	rml_uint_t len = strlen(str); /* string lenght */
	rml_uint_t lenwhat = strlen(what); /* string lenght */
	int i = 0;
	int j = len;
	int l = 0;
	if (!lenwhat) RML_TAILCALLK(rmlSC);
	for (; i < len;) if (!strncmp(str+i,what,lenwhat)) i+=lenwhat; else break;
	for (; j >= 0; ) if (!strncmp(str+j-lenwhat,what,lenwhat)) j-=lenwhat; else break;
	/* printf("i=%d, j=%d, lenwhat=%d, len=%d", i, j, lenwhat, len); */
	if (j <= i) /* the string is actually only whitespace, return "" */
	{
		/* alloc the new string */
		struct rml_string *strnew = rml_prim_mkstring(0, 2);
		unsigned char *snew = (unsigned char*)strnew->data;
		snew[0] = '\0';
		rmlA0 = RML_TAGPTR(strnew);
		RML_TAILCALLK(rmlSC);
	}
	/* alloc the new string */
	struct rml_string *strnew = rml_prim_mkstring(j-i, 2);
	unsigned char *snew = (unsigned char*)strnew->data;
    str = RML_STRINGDATA(rmlA0);
	for(l=i; l < j; l++)
		*snew++ = str[l];
	*snew = '\0';
	rmlA0 = RML_TAGPTR(strnew);
	/*
	printf("sss:%s|\nstr:%s|\ndlm:%s|\nact:",str, RML_STRINGDATA(rmlA0), RML_STRINGDATA(rmlA1));
	fwrite(RML_STRINGDATA(rmlA0), RML_HDRSTRLEN(RML_GETHDR(rmlA0)), 1, stdout);
	printf("|\n");
	*/
	RML_TAILCALLK(rmlSC);
}
void *mk_box8(unsigned ctor, void *x0, void *x1, void *x2, void *x3, void *x4,
    void *x5, void *x6, void *x7) {
  struct rml_struct *p = alloc_words(9);
  p->header = RML_STRUCTHDR(8, ctor);
  p->data[0] = x0;
  p->data[1] = x1;
  p->data[2] = x2;
  p->data[3] = x3;
  p->data[4] = x4;
  p->data[5] = x5;
  p->data[6] = x6;
  p->data[7] = x7;
  return RML_TAGPTR(p);
}
Exemple #22
0
RML_END_LABEL


RML_BEGIN_LABEL(External__trim)
{
	char *str = RML_STRINGDATA(rmlA0);
	rml_uint_t len = strlen(str); /* string lenght */
	int i = 0;
	int j = len-1;
	int l = 0;
	for (; i < len;) if (isspace(str[i])) i++; else break;
	for (; j >= 0; ) if (isspace(str[j])) j--; else break;
	/* printf("i=%d, j=%d", i, j); */
	if (j <= i) /* the string is actually only whitespace, return "" */
	{
		/* alloc the new string */
		struct rml_string *strnew = rml_prim_mkstring(0, 1);
		unsigned char *snew = (unsigned char*)strnew->data;
		snew[0] = '\0';
		rmlA0 = RML_TAGPTR(strnew);
		RML_TAILCALLK(rmlSC);
	}
	/* alloc the new string */
	struct rml_string *strnew = rml_prim_mkstring(j-i+1, 1);
	unsigned char *snew = (unsigned char*)strnew->data;
    str = RML_STRINGDATA(rmlA0);
	for(l=i; l <= j; l++)
		*snew++ = str[l];
	*snew = '\0';
	rmlA0 = RML_TAGPTR(strnew);
	/*
	printf("sss:%s|\nstr:%s|\nact:",str, RML_STRINGDATA(rmlA0));
	fwrite(RML_STRINGDATA(rmlA0), RML_HDRSTRLEN(RML_GETHDR(rmlA0)), 1, stdout);
	printf("|\n");
	*/
	RML_TAILCALLK(rmlSC);
}
Exemple #23
0
RML_END_LABEL


RML_BEGIN_LABEL(RML__list_5fmap)
{
    rml_uint_t nelts;
    void *lst = rmlA0;
    void *function = rmlA1;

    /* count the number of elements in the first list */
    nelts = 0;
    while( RML_GETHDR(lst) == RML_CONSHDR ) {
      lst = RML_CDR(lst);
      ++nelts;
    }
    /* call the relation, to build the second list  */
    if( nelts == 0 )
    { /* do nothing, return nil */ }
    else 
    {
        void **chunk = (void**)rml_prim_alloc(3*nelts, 2);
        lst = rmlA0;
        rmlA0 = RML_TAGPTR(chunk);
        do {
            chunk[0] = RML_IMMEDIATE(RML_CONSHDR);
            rmlA0 = RML_CAR(lst); /* element */;
            RML_TAILCALL(rmlA1 /* fn */,1);
            chunk[1] = rmlA0;
            chunk[2] = RML_TAGPTR(chunk + 3);
            lst = RML_CDR(lst);
            chunk += 3;
        } while( --nelts != 0 );
    }

    /* return resulting list */
    RML_TAILCALLK(rmlSC);
}
void *mk_nil(void) {
  return RML_TAGPTR(&rml_prim_nil);
}
Exemple #25
0
RML_END_LABEL

RML_BEGIN_LABEL(External__commentEscape)
{
	char *str = RML_STRINGDATA(rmlA0);
	rml_uint_t len = strlen(str);
	char *strnew = (char*)malloc(2*len); /* should be enough */
	int i=0, j=0, c=0, n=0;
	rml_uint_t lennew = 0;
	unsigned char *snew = 0;
	struct rml_string *rml_strnew = 0;
	while (1)
	{
		if (i >= len) break;
		c = str[i];
		if (c == '\\')
		{
			strnew[lennew] = c; lennew++; i++;
			if (i >= len) break;
			c = str[i];
			switch (c)
			{
			/*
			case 'n':
				strnew[lennew] = '\n';
				break;
			case 't':
				strnew[lennew] = '\t';
				break;
			case 'r':
				strnew[lennew] = '\r';
				break;
			*/
			case '\\':
				strnew[lennew] = '\\';
				break;
			case '"':
				/* already escaped */
				strnew[lennew] = '"';
				break;
			default: /* don't know the escape sequence, escape the \ */
				strnew[lennew] = '\\';
				lennew++;
				strnew[lennew] = c;
			}
			lennew++;
		}
		else if(c == '"' || c == '\'')
		{
			strnew[lennew++] = '\\';
			strnew[lennew++] = c;
		}
		else
		{
			strnew[lennew++] = c;
		}
		i++;
	}
	strnew[lennew]='\0';
	/* now alloc the new string */
	rml_strnew = rml_prim_mkstring(lennew, 1);
	snew = (unsigned char*)rml_strnew->data;
	for(i = 0; i < lennew; i++)
	{
		*snew++ = strnew[i];
	}
	*snew = '\0';
	free(strnew);
	rmlA0 = RML_TAGPTR(rml_strnew);
	RML_TAILCALLK(rmlSC);
}
Exemple #26
0
RML_END_LABEL

RML_BEGIN_LABEL(External__stringEscape)
{
	char *str = RML_STRINGDATA(rmlA0);
	rml_uint_t len = strlen(str);
	char *strnew = (char*)malloc(2*len); /* should be enough */
	int i=0, j=0, c=0, n=0;
	rml_uint_t lennew = 0;
	unsigned char *snew = 0;
	struct rml_string *rml_strnew = 0;
	while (1)
	{
		if (i >= len) break;
		c = str[i];
		if (c == '\\')
		{
			strnew[lennew] = c; lennew++; i++;
			if (i >= len) break;
			c = str[i];
			switch (c)
			{
			case 'f':
				strnew[lennew] = '\f';
				break;
			case 'b':
				strnew[lennew] = '\b';
				break;
			case 'e':
				strnew[lennew] = 033;
				break;
			case 'v':
				strnew[lennew] = '\v';
				break;
			case 'a':
				strnew[lennew] = '\a';
				break;
			case '?':
				strnew[lennew] = '\?';
				break;
			case 'n':
				strnew[lennew] = '\n';
				break;
			case 't':
				strnew[lennew] = '\t';
				break;
			case 'r':
				strnew[lennew] = '\r';
				break;
			case '"':
				strnew[lennew] = '\"';
				break;
			default:
				strnew[lennew] = '\\'; lennew++;
				strnew[lennew] = c;
			}
			lennew++;
		}
		else if(c == '"' || c == '\'')
		{
			strnew[lennew++] = '\\';
			strnew[lennew++] = c;
		}
		else /* leave the char as it is */
		{
			strnew[lennew++] = c;
		}
		i++;
	}
	strnew[lennew]='\0';
	/* now alloc the new string */
	rml_strnew = rml_prim_mkstring(lennew, 1);
	snew = (unsigned char*)rml_strnew->data;
	for(i = 0; i < lennew; i++)
	{
		*snew++ = strnew[i];
	}
	*snew = '\0';
	free(strnew);
	rmlA0 = RML_TAGPTR(rml_strnew);
	RML_TAILCALLK(rmlSC);
}
void *mk_box0(unsigned ctor) {
  struct rml_struct *p = alloc_words(1);
  p->header = RML_STRUCTHDR(0, ctor);
  return RML_TAGPTR(p);
}
void *mk_none(void) {
  static struct rml_header none = { RML_STRUCTHDR(0, 0) };
  return RML_TAGPTR(&none);
}
Exemple #29
0
RML_END_LABEL


RML_BEGIN_LABEL(External__strrplall)
{
	rml_uint_t len1 = RML_HDRSTRLEN(RML_GETHDR(rmlA0)); /* string lenght */
	rml_uint_t len2 = RML_HDRSTRLEN(RML_GETHDR(rmlA1)); /* string lenght */
	rml_uint_t len3 = RML_HDRSTRLEN(RML_GETHDR(rmlA2)); /* string lenght */
	char *str1 = RML_STRINGDATA(rmlA0);
	char *str2 = RML_STRINGDATA(rmlA1);
	char *str3 = RML_STRINGDATA(rmlA2);
	char *strpos;
	if (len1 == 0 || len2==0)
	{
		rmlA0 = rmlA0; /* return the first string unchanged */
		RML_TAILCALLK(rmlSC);
	}
	if ((strpos = strstr(str1, str2)) == NULL) /* the string is not there */
	{
		rmlA0 = rmlA0; /* return the first string unchanged */
		RML_TAILCALLK(rmlSC);
	}
	else
	{
		/* string is there */
		rml_uint_t len = 0;
		/* find where */
		rml_uint_t pos = (int)(strpos - str1);
		rml_uint_t count = 1; /* we already find it once above */
		/* how many times the string is there? */
		strpos += len2; /* advance the position */
		/*
		printf ("str1 [%s], str2[%s], str3[%s]\n", str1, str2, str3);
		printf ("strpos:%s\n", strpos);
		*/
		/* how many times the string is there? */
		while ((strpos = strstr(strpos, str2)) != NULL)
		{
			count++;
			/* printf ("strpos:%s\n", strpos); */
			strpos += len2;
		}
		/* calculate the lenght of the new string */
		len = len1+(len3-len2)*count;
		/* print len
		printf("len:%d, len1:%d, len2:%d, len3:%d, count:%d\n", len, len1, len2, len3, count);
		*/
		/* now alloc the new string */
		struct rml_string *strnew = rml_prim_mkstring(len, 3);
		int i, j, k;
		/* reread the rmlAX, it could have been moved by the GC */
		str1 = RML_STRINGDATA(rmlA0);
		str2 = RML_STRINGDATA(rmlA1);
		str3 = RML_STRINGDATA(rmlA2);
		unsigned char *snew = (unsigned char*)strnew->data;
		/* until pos, use the first string */
		/* go to first */
		strpos = strstr(str1, str2);
		pos = (int)(strpos - str1);
		do
		{
			/* until pos, use the first string */
			/* printf("pos1:%d\n", pos); */
			for(i=0; i < pos; i++)
			{
				*snew++ = str1[i];
			}
			for(i=0; i < len3; i++)
			{
				*snew++ = str3[i];
			}
			/* move the str1 pointer after str2 */
			str1 += (pos+len2);
			strpos = strstr(str1, str2);
			if (!strpos)
			{
				/* copy stuff left from str1 */
				for(i=0; i < strlen(str1); i++)
				{
					*snew++ = str1[i];
				}
				break;
			}
			pos = (int)(strpos - str1);
			/* printf("pos2:%d and str1:%s\n", pos, str1); */
		}
		while (1);
		*snew = '\0';
		rmlA0 = RML_TAGPTR(strnew);
		RML_TAILCALLK(rmlSC);
	}
}