//## @Const method String[] String.split(RegExp regex); static KMETHOD String_split(KonohaContext *kctx, KonohaStack *sfp) { INIT_GCSTACK(); kArray *resultArray = (kArray *)KLIB new_kObject(kctx, _GcStack, KGetReturnType(sfp), 0); kArray_split(kctx, resultArray, sfp[0].asString, sfp[1].asRegExp, S_size(sfp[0].asString)); KReturnWith(resultArray, RESET_GCSTACK()); }
//## String Curl.receiveString(); static KMETHOD Curl_receiveString(KonohaContext *kctx, KonohaStack *sfp) { kCurl* kcurl = (kCurl *)sfp[0].asObject; /* presets */ struct ReceiveBuffer rbuf = {0}; rbuf.kctx = kctx; KLIB KBuffer_Init(&(kctx->stack->cwb), &rbuf.wb); curl_easy_setopt(kcurl->curl, CURLOPT_WRITEFUNCTION, writeToBuffer); curl_easy_setopt(kcurl->curl, CURLOPT_WRITEDATA, &rbuf); /* perform */ KMakeTrace(trace, sfp); CURLcode res; if(kcurl->headers != NULL) { curl_easy_setopt(kcurl->curl, CURLOPT_HTTPHEADER, kcurl->headers); } KTraceResponseCheckPoint(trace, 0, "curl_easy_perform", res = curl_easy_perform(kcurl->curl) ); if(res != CURLE_OK) { int fault = diagnosisCurlFaultType(kctx, res, (kcurl->URLInfoNULL == NULL) ? 0 : kString_guessUserFault(kcurl->URLInfoNULL)); KTraceErrorPoint(trace, fault, "curl_easy_perform", LogURL(kcurl), LogCurlStrError(res)); } KReturnWith( KLIB new_kString(rbuf.kctx, OnStack, KLIB KBuffer_Stringfy(rbuf.kctx, &rbuf.wb, 0), KBuffer_bytesize(&rbuf.wb), 0), KLIB KBuffer_Free(&rbuf.wb) ); }
//## 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)); }
//## @Const method String[] String.split(RegExp regex, Int limit); static KMETHOD String_splitWithLimit(KonohaContext *kctx, KonohaStack *sfp) { INIT_GCSTACK(); size_t limit = sfp[2].intValue < 0 ? S_size(sfp[0].asString) : (size_t) sfp[2].intValue; kArray *resultArray = (kArray *)KLIB new_kObject(kctx, _GcStack, KGetReturnType(sfp), 0); kArray_split(kctx, resultArray, sfp[0].asString, sfp[1].asRegExp, limit); KReturnWith(resultArray, RESET_GCSTACK()); }
//## ResultSet Connection.query(String query); static KMETHOD Connection_query(KonohaContext *kctx, KonohaStack *sfp) { INIT_GCSTACK(); KMakeTrace(trace, sfp); kConnection *conn = (kConnection *)sfp[0].asObject; const char *query = kString_text(sfp[1].asString); kResultSet *rs = (kResultSet *)KLIB new_kObject(kctx, OnStack, KGetReturnType(sfp), (uintptr_t)conn); KCursor *qcur = conn->driver->qexec(kctx, conn->db, query, rs, trace); if(qcur != NULL) { rs->qcur = qcur; rs->driver = conn->driver; } KReturnWith(rs, RESET_GCSTACK()); }
//## Bytes String.toBytes(); static KMETHOD String_toBytes(KonohaContext *kctx, KonohaStack *sfp) { kString* thisString = sfp[0].asString; size_t size = kString_size(thisString); if(PLATAPI I18NModule.isSystemCharsetUTF8(kctx)) { KReturn(new_kBytes(kctx, OnStack, KGetReturnType(sfp), kString_text(thisString), size)); } else { KMakeTrace(trace, sfp); KBuffer wb; KLIB KBuffer_Init(&(kctx->stack->cwb), &wb); KBuffer_convertCharset(kctx, &wb, I18NAPI systemCharset, "UTF-8", kString_text(thisString), size, trace); KReturnWith( new_kBytes(kctx, OnStack, KGetReturnType(sfp), KLIB KBuffer_text(kctx, &wb, NonZero), KBuffer_bytesize(&wb)), KLIB KBuffer_Free(&wb) ); } }