//## String DIR.readPath() static KMETHOD DIR_readPath(KonohaContext *kctx, KonohaStack *sfp) { kDir *dir = (kDir *)sfp[0].asObject; if(dir->dirp != NULL) { KMakeTrace(trace, sfp); struct dirent entry, *result; int ret = readdir_r(dir->dirp, &entry, &result); if(result != NULL) { char *d_name = result->d_name, delim[2] = {'/', 0}; KGrowingBuffer wb; KLIB Kwb_Init(&(kctx->stack->cwb), &wb); KLIB Kwb_Write(kctx, &wb, S_text(dir->PathInfoNULL), S_size(dir->PathInfoNULL)); KLIB Kwb_Write(kctx, &wb, delim, 1); if(dir->readerIconv != ICONV_NULL) { KLIB Kwb_Write(kctx, &wb, d_name, strlen(d_name)); } else { KLIB Kwb_iconv(kctx, &wb, dir->readerIconv, d_name, strlen(d_name), trace); } KReturnWith( KLIB new_kString(kctx, OnStack, KLIB Kwb_top(kctx, &wb, 0), Kwb_bytesize(&wb), StringPolicy_SystemInfo), KLIB Kwb_Free(&wb) ); } if(ret == -1) { KTraceErrorPoint(trace, SystemFault, "readdir", LogErrno); } kDir_close(kctx, dir); } KReturn(KNULL(String)); }
static void dumpMethod(KonohaContext *kctx, KonohaStack *sfp, kMethod *mtd) { KGrowingBuffer wb; KLIB Kwb_Init(&(kctx->stack->cwb), &wb); kMethod_WriteToBuffer(kctx, mtd, &wb); PLATAPI printf_i("%s\n", KLIB Kwb_top(kctx, &wb, 1)); KLIB Kwb_Free(&wb); return; }
//## @Const method String String.replace(RegExp searchvalue, String newvalue); static KMETHOD String_replace(KonohaContext *kctx, KonohaStack *sfp) { kString *s0 = sfp[0].asString; kRegExp *re = sfp[1].asRegExp; const char* fmttext = S_text(sfp[2].asString); size_t fmtlen = S_size(sfp[2].asString); kString *s = s0; if(IS_NOTNULL(re) && S_size(re->pattern) > 0) { KGrowingBuffer wb; KLIB Kwb_Init(&(kctx->stack->cwb), &wb); const char *str = S_text(s0); // necessary const char *base = str; const char *eos = str + S_size(s0); // end of str kregmatch_t pmatch[KREGEXP_MATCHSIZE+1]; int isGlobalOption = RegExp_isGlobal(re); do { if(str >= eos) break; int res = pcre_regexec(kctx, re->reg, str, KREGEXP_MATCHSIZE, pmatch, re->eflags); if(res != 0) { // TODO //LOG_regex(kctx, sfp, res, re, str); break; } size_t len = pmatch[0].rm_eo; if(pmatch[0].rm_so > 0) { KLIB Kwb_write(kctx, &wb, str, pmatch[0].rm_so); } size_t matched = knh_regexp_Matched(pmatch, KREGEXP_MATCHSIZE); if(len > 0) { Kwb_writeRegexFormat(kctx, &wb, fmttext, fmtlen, base, pmatch, matched); str += len; } else { if(str == base) { // 0-length match at head of string Kwb_writeRegexFormat(kctx, &wb, fmttext, fmtlen, base, pmatch, matched); } break; } } while(isGlobalOption); KLIB Kwb_write(kctx, &wb, str, strlen(str)); // write out remaining string s = Kwb_newString(kctx, OnStack, &wb); // close cwb KLIB Kwb_Free(&wb); } KReturn(s); }
static KMETHOD Statement_import(KonohaContext *kctx, KonohaStack *sfp) { int ret = false; VAR_Statement(stmt, gma); kTokenArray *tokenList = (kTokenArray *) kStmt_GetObjectNULL(kctx, stmt, KW_TokenPattern); if(tokenList == NULL) { KReturnUnboxValue(false); } kNameSpace *ns = Stmt_ns(stmt); SugarSyntaxVar *syn = (SugarSyntaxVar *) SYN_(ns, KW_ExprMethodCall); kExpr *expr; kTokenVar *tkImport = /*G*/new_(TokenVar, 0, OnGcStack); tkImport->resolvedSymbol = MN_("import"); if(IS_Token(tokenList)) { kTokenArray *list = ((kToken *) tokenList)->subTokenList; if(IS_String(list)) { /* case: import cstyle; */ kString *pkgname = (kString *) list; expr = CreateImportCall(kctx, syn, tkImport, ns, pkgname); } else if(kArray_size(list) == 1) { /* case : import("konoha.import"); */ kExpr *param0 = makeStringConstValue(kctx, list->TokenItems[0]->text); expr = SUGAR new_UntypedCallStyleExpr(kctx, syn, 3, tkImport, new_ConstValueExpr(kctx, O_typeId(ns), UPCAST(ns)), param0); } else if(kArray_size(list) == 2) { /* case : import("konoha.import", "import"); */ kExpr *param0 = makeStringConstValue(kctx, list->TokenItems[0]->text); kExpr *param1 = makeStringConstValue(kctx, list->TokenItems[1]->text); expr = SUGAR new_UntypedCallStyleExpr(kctx, syn, 4, tkImport, new_ConstValueExpr(kctx, O_typeId(ns), UPCAST(ns)), param0, param1); } else { KReturnUnboxValue(false); } } else { KGrowingBuffer wb; KLIB Kwb_Init(&(kctx->stack->cwb), &wb); /* case : import konoha.import */ ksymbol_t star = SYM_("*"); size_t i = 0; if(i + 2 < kArray_size(tokenList)) { for (; i < kArray_size(tokenList)-1; i+=2) { /* name . */ kToken *tk = tokenList->TokenItems[i+0]; if(i+2 < kArray_size(tokenList)) { kToken *startTk = tokenList->TokenItems[i+2]; if(startTk->resolvedSyntaxInfo->keyword == star) { break; } } KLIB Kwb_Write(kctx, &wb, S_text(tk->text), S_size(tk->text)); KLIB Kwb_Write(kctx, &wb, ".", 1); } } kString *name = tokenList->TokenItems[i]->text; KLIB Kwb_Write(kctx, &wb, S_text(name), S_size(name)); kString *pkgname = KLIB new_kString(kctx, OnGcStack, KLIB Kwb_top(kctx, &wb, 1), Kwb_bytesize(&wb), 0); expr = CreateImportCall(kctx, syn, tkImport, ns, pkgname); } KLIB kObject_setObject(kctx, stmt, KW_ExprPattern, TY_Expr, expr); ret = SUGAR kStmt_TypeCheckByName(kctx, stmt, KW_ExprPattern, gma, TY_void, TypeCheckPolicy_ALLOWVOID); if(ret) { kStmt_typed(stmt, EXPR); } KReturnUnboxValue(ret); }