Ejemplo n.º 1
0
static METHOD knh__String_replace(Ctx *ctx, knh_sfp_t *sfp)
{
	knh_bytes_t base = knh_String_tobytes(sfp[0].s);
	knh_bytes_t target = knh_String_tobytes(sfp[1].s);
	knh_bytes_t alt = knh_String_tobytes(sfp[2].s);
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	int search_flag= 0, ch = target.buf[0], i;

	if (base.len == 0 || target.len == 0) KNH_RETURN(ctx, sfp, sfp[0].o);
	for(i = 0; i < base.len - target.len+1; i++) {
		if(base.buf[i] == ch && knh_bytes_equals_(base, i, target)) {
		    knh_Bytes_write(ctx, cwb->ba, alt);
			i += target.len - 1;
			search_flag = 1;
		}else {
			knh_Bytes_putc(ctx, cwb->ba, base.buf[i]);
		}
	}
	if(search_flag == 0) {
		KNH_RETURN(ctx, sfp, sfp[0].o);
	}
	else {
		knh_bytes_t leftover = {base.buf + i, base.len - i};
		knh_Bytes_write(ctx, cwb->ba, leftover);
		KNH_RETURN(ctx, sfp, knh_cwb_newString(ctx, cwb));
	}
}
Ejemplo n.º 2
0
static METHOD Method_getName(Ctx *ctx, knh_sfp_t *sfp, long rix)
{
	
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	knh_write_mn(ctx, cwb->w, DP(sfp[0].mtdOBJ)->mn);
	RETURN_(knh_cwb_newString(ctx, cwb));
}
Ejemplo n.º 3
0
static METHOD knh__String_opSub(Ctx *ctx, knh_sfp_t *sfp)
{
	knh_bytes_t base = knh_String_tobytes(sfp[0].s);
	knh_bytes_t t = knh_String_tobytes(sfp[1].s);
	knh_uchar_t c = t.buf[0];
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	size_t i;
	for(i = 0; i < base.len; i++) {
		if(base.buf[i] == c) {
			size_t j;
			for(j = 1; j < t.len; j++) {
				if(base.buf[i+j] != t.buf[j]) break;
			}
			if(j == t.len) {
				i += t.len - 1;
				continue;
			}
		}
		knh_Bytes_putc(ctx, cwb->ba, base.buf[i]);
	}
	if(base.len == knh_cwb_size(cwb)) {
		knh_cwb_close(cwb);
		KNH_RETURN(ctx, sfp, sfp[0].o);
	}
	else {
		KNH_RETURN(ctx, sfp, knh_cwb_newString(ctx, cwb));
	}
}
Ejemplo n.º 4
0
knh_String_t* knh_InputStream_readLine(Ctx *ctx, knh_InputStream_t *in)
{
	int ch;
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	if(DP(in)->decNULL == NULL && in->dspi->getCharset != NULL) {
		char *charset = (char*)in->dspi->getCharset(ctx, DP(in)->fd);
		if(charset != NULL) {
			knh_InputStream_setCharset(ctx, in, new_StringDecoderNULL(ctx, B(charset), NULL));
		}
	}
	while((ch = knh_InputStream_getc(ctx, in)) != EOF) {
		if(ch == '\r') {
			DP(in)->prev = ch;
			goto L_TOSTRING;
		}
		else if(ch == '\n') {
			if(DP(in)->prev == '\r') continue;
			DP(in)->prev = ch;
			goto L_TOSTRING;
		}
		else {
			knh_Bytes_putc(ctx, cwb->ba, ch);
		}
	}
	L_TOSTRING:;
	if(knh_cwb_size(cwb) != 0) {
		if(DP(in)->decNULL == NULL) {
			return knh_cwb_newString(ctx, cwb);
		}
		else {
			return knh_cwb_newStringDECODE(ctx, cwb, DP(in)->decNULL);
		}
	}
	return KNH_TNULL(String);
}
Ejemplo n.º 5
0
static METHOD knh__String_concat(Ctx *ctx, knh_sfp_t *sfp)
{
	int i, ac = knh_stack_argc(ctx, sfp);
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	knh_sfp_t *esp = KNH_LOCAL(ctx);
	for(i = 0; i < ac; i++) {
		if(IS_bString(sfp[i].o)) {
			knh_Bytes_write(ctx, cwb->ba, knh_String_tobytes(sfp[i].s));
		}
		else {
			KNH_ASSERT(esp == ctx->esp);
			KNH_SETv(ctx, esp[1].o, sfp[i].o); esp[1].data = sfp[i].data;
			knh_esp1_format(ctx, METHODN__s, cwb->w, KNH_NULL);
		}
	}
	KNH_RETURN(ctx, sfp, knh_cwb_newString(ctx, cwb));
}
Ejemplo n.º 6
0
static METHOD knh__String_opMul(Ctx *ctx, knh_sfp_t *sfp)
{
	knh_intptr_t n = p_int(sfp[1]);
	if(n <= 0) {
		KNH_RETURN(ctx, sfp, TS_EMPTY);
	}else if(n == 1) {
		KNH_RETURN(ctx, sfp, sfp[0].o);
	}
	else {
		knh_bytes_t base = knh_String_tobytes(sfp[0].s);
		knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
		knh_intptr_t i;
		for(i = 0; i < n; i++) {
			knh_Bytes_write(ctx, cwb->ba, base);
		}
		KNH_RETURN(ctx, sfp, knh_cwb_newString(ctx, cwb));
	}
}
Ejemplo n.º 7
0
static METHOD knh__String_opAdd(Ctx *ctx, knh_sfp_t *sfp)
{
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	knh_sfp_t *esp = KNH_LOCAL(ctx);
	if(IS_bString(sfp[0].o)) {
		knh_Bytes_write(ctx, cwb->ba, knh_String_tobytes(sfp[0].s));
	}
	else {
		KNH_SETv(ctx, esp[1].o, sfp[0].o); esp[1].data = sfp[0].data;
		knh_esp1_format(ctx, METHODN__s, cwb->w, KNH_NULL);
	}
	if(IS_bString(sfp[1].o)) {
		knh_Bytes_write(ctx, cwb->ba, knh_String_tobytes(sfp[1].s));
	}
	else {
		KNH_ASSERT(esp == ctx->esp);
		KNH_SETv(ctx, esp[1].o, sfp[1].o); esp[1].data = sfp[1].data;
		knh_esp1_format(ctx, METHODN__s, cwb->w, KNH_NULL);
	}
	KNH_RETURN(ctx, sfp, knh_cwb_newString(ctx, cwb));
}
Ejemplo n.º 8
0
static knh_String_t *Gamma_vperror(CTX ctx, int pe, const char *fmt, va_list ap)
{
	knh_String_t *msg = TS_EMPTY;
	int isPRINT = (pe <= KC_DWARN) ? 1 : 0;
	if(pe != KC_DEBUG && (CTX_isInteractive(ctx) || knh_isCompileOnly(ctx))) {
		isPRINT = 1;
	}
	if(Gamma_isQuiet(ctx->gma) || ctx->gma->uline == 0) {
		isPRINT = 0;
	}
	//DBG_P("/*isPRINT=%d*/ uline=%d", isPRINT, ctx->gma->uline);
	if(isPRINT == 1) {
		knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
		knh_write_uline(ctx, cwb->w, ctx->gma->uline);
		knh_write_ascii(ctx, cwb->w, KC__(pe));
		knh_vprintf(ctx, cwb->w, fmt, ap);
		msg = knh_cwb_newString(ctx, cwb);
		knh_Array_add(ctx, DP(ctx->gma)->errmsgs, msg);
		fprintf(stderr, "%s - %s%s\n", TERM_BNOTE(ctx, pe), S_tochar(msg), TERM_ENOTE(ctx, pe));
	}
	return msg;
}
Ejemplo n.º 9
0
METHOD knh__System_setMethodCompilationListener(Ctx *ctx, knh_sfp_t *sfp)
{
	String *key;
	if(IS_NULL(sfp[2].s)) {
		key = T__("MethodC");
	}
	else {
		knh_cwb_t cwbbuf, *cwb = knh_cwb_openinit(ctx, &cwbbuf, STEXT("MethodC"));
		knh_bytes_t anno = knh_String_tobytes(sfp[2].s);
		if(anno.buf[0] != '@') {
			knh_cwb_putc(ctx, cwb, '@');
		}
		knh_cwb_write(ctx, cwb, anno);
		key = knh_cwb_newString(ctx, cwb);
	}
	KNH_LOCK(ctx, LOCK_SYSTBL, NULL);
	{
		DictMap *dm = DP(ctx->sys)->listenerDictMap;
		knh_DictMap_set(ctx, dm, key, sfp[1].o);
	}
	KNH_UNLOCK(ctx, LOCK_SYSTBL, NULL);
	KNH_RETURN_void(ctx, sfp);
}
Ejemplo n.º 10
0
static knh_sfp_t *knh_Exception_addStackTrace(Ctx *ctx, knh_Exception_t *e, knh_sfp_t *sfp)
{
	knh_intptr_t shift = sfp[K_SHIFTIDX].shift;
	knh_sfp_t *psfp = sfp - shift;
	if(ctx->stack < psfp && psfp < sfp) {
		if(psfp[K_MTDIDX].callmtd != NULL && isCalledMethod(ctx, psfp + K_MTDIDX)) {
			knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
			knh_opline_t *pc = psfp[K_SHIFTIDX].pc;
			knh_Method_t *mtd = psfp[K_MTDIDX].callmtd;
			int linenum = pc->line;
			const char *file = knh_Method_file(ctx, mtd);
			knh_write_cline(ctx, cwb->w, file, linenum);
			knh_putc(ctx, cwb->w, ':');
			knh_stack_writeStackTrace(ctx, sfp, cwb->w);
			if(DP(e)->tracesNULL == NULL) {
				KNH_INITv(DP(e)->tracesNULL, new_Array(ctx, CLASS_String, 0));
			}
			knh_Array_add(ctx, DP(e)->tracesNULL, knh_cwb_newString(ctx, cwb));
			sfp = psfp;
		}
	}
	return sfp - 1;
}
Ejemplo n.º 11
0
static knh_String_t *new_String__float(Ctx *ctx, knh_float_t n)
{
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	knh_write_ffmt(ctx, cwb->w, K_FLOAT_FMT, n);
	return knh_cwb_newString(ctx, cwb);
}
Ejemplo n.º 12
0
static knh_String_t *new_String__int(Ctx *ctx, knh_int_t n)
{
	knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf);
	knh_write_ifmt(ctx, cwb->w, K_INT_FMT, n);
	return knh_cwb_newString(ctx, cwb);
}