builtins.cpp
来自「Shorthand是一个强大的脚本语言」· C++ 代码 · 共 710 行 · 第 1/2 页
CPP
710 行
* finds position of one string within another string.
* returns (-1) if no match was found.
*/
/*
SHORTHAND_BUILTIN(strpos, 2)
{
string haystack, needle;
ARG(0)->toString(haystack);
ARG(1)->toString(needle);
const char* match = strstr(haystack, needle);
if (match != NULL) return match - (const char*) haystack;
else return -1;
}*/
/**
* URL-encodes the string (replaces certain characters with %NN sequences).
*/
SHORTHAND_BUILTIN(urlencode, 1)
{
string s,t;
ARG(0)->toString(s);
s.urlencode(t);
ShhValue tmp(MAKE_STRING(t));
return tmp;
}
/**
* URL-decodes the string (replaces %NN sequences by corresponding characters).
*/
SHORTHAND_BUILTIN(urldecode, 1)
{
string s,t;
ARG(0)->toString(s);
s.urldecode(t);
ShhValue tmp(MAKE_STRING(t));
return tmp;
}
/**
* Returns current datetime.
*/
SHORTHAND_BUILTIN(now, 0)
{
datetime now;
ShhValue tmp(now);
return tmp;
}
/**
* date(x) - tries to convert any value to date.
* date(y,m,d) - assembles date from year,month, day
* returns date value
*/
SHORTHAND_BUILTIN(date, 1)
{
ShhValue result;
datetime dt;
if (ARGC == 3)
{
int year = ARG(0)->toInt();
int month = ARG(1)->toInt();
int day = ARG(2)->toInt();
dt.import_ydm(year, month, day);
}
else if (ARGC == 1)
{
ARG(0)->toDate(dt);
}
else
{
throw new ShhObjectException(4110, "function DATE requres either one or three parameters");
}
result.setDate(dt);
return result;
}
/**
* formatdate(dt,format)
*
* formats date using strftime-compatible format
*/
SHORTHAND_BUILTIN(formatdate, 2)
{
string format;
datetime dt;
ARG(0)->toDate(dt);
ARG(1)->toString(format);
string s;
dt.format(format, s);
ShhValue result(MAKE_STRING(s));
return result;
}
SHORTHAND_BUILTIN(addseconds, 2)
{
datetime dt;
int delta;
ARG(0)->toDate(dt);
delta = ARG(1)->toInt();
dt.add_seconds(delta);
ShhValue result(dt);
return result;
}
SHORTHAND_BUILTIN(addminutes, 2)
{
datetime dt;
int delta;
ARG(0)->toDate(dt);
delta = ARG(1)->toInt() * 60;
dt.add_seconds(delta);
ShhValue result(dt);
return result;
}
SHORTHAND_BUILTIN(addhours, 2)
{
datetime dt;
int delta;
ARG(0)->toDate(dt);
delta = ARG(1)->toInt() * 3600;
dt.add_seconds(delta);
ShhValue result(dt);
return result;
}
SHORTHAND_BUILTIN(getcookie, 1)
{
string name;
ARG(0)->toString(name);
const char* value = module->stream()->http_get_cookie(name);
ShhValue result(value);
return value;
}
SHORTHAND_BUILTIN(setcookie, 1)
{
ShhObject* o = ARG(0)->toObject();
if (stricmp(o->getTypeName(), "COOKIE") != 0)
throw new ShhObjectException(1660, "argument to setCookie() must be cookie object");
if (module->stream()->http_headers_sent())
{
throw new ShhFunctionException(9012, "Cannot set cookie value because headers have been already sent");
}
const CHttpCookie* cookie = ((ShhCookie*) o)->getCookie();
module->stream()->http_set_cookie(cookie);
return ShhValue::EMPTY;
}
SHORTHAND_BUILTIN(FileExists, 1)
{
string name;
ARG(0)->toString(name);
ShhValue result;
struct stat sst;
if (::stat(name, &sst) == 0)
{
result = 1;
}
else
{
result = 0;
}
return result;
}
SHORTHAND_BUILTIN(FileSize, 1)
{
string name;
ARG(0)->toString(name);
ShhValue result;
struct stat sst;
if (::stat(name, &sst) == 0)
{
result = sst.st_size;
}
else
{
result = -1;
}
return result;
}
SHORTHAND_BUILTIN(translate, 3)
{
string source, ch1, ch2;
ARG(0)->toString(source);
ARG(1)->toString(ch1);
ARG(2)->toString(ch2);
if (ch1.length() < 1)
throw new ShhFunctionException(9013, "second argument to translate() must have at least one character");
if (ch2.length() < 1)
throw new ShhFunctionException(9013, "third argument to translate() must have at least one character");
source.translate(ch1.first(), ch2.first());
ShhValue result(MAKE_STRING(source));
return result;
}
SHORTHAND_BUILTIN(replace, 3)
{
string source, ch1, ch2;
ARG(0)->toString(source);
ARG(1)->toString(ch1);
ARG(2)->toString(ch2);
if (ch1.length() < 1)
throw new ShhFunctionException(9014, "second argument to replace() must have at least one character");
source.replace_all(ch1, ch2);
ShhValue result;
result = MAKE_STRING(source);
return result;
}
SHORTHAND_BUILTIN(strpos, 2)
{
string source, needle;
int offset;
ARG(0)->toString(source);
ARG(1)->toString(needle);
if (ARGC > 2) offset = ARG(2)->toInt();
else offset = 0;
int pos = needle.is_empty() ? -1 : source.strpos(needle, offset);
ShhValue result(pos);
return result;
}
SHORTHAND_BUILTIN(int, 1)
{
int x = ARG(0)->toInt();
ShhValue result(x);
return result;
}
SHORTHAND_BUILTIN(float, 1)
{
double x = ARG(0)->toFloat();
ShhValue result(x);
return result;
}
SHORTHAND_BUILTIN(string, 1)
{
ShhValue* arg = ARG(0);
if (arg->isString()) return *arg;
string x;
arg->toString(x);
ShhValue result(x);
return result;
}
SHORTHAND_BUILTIN(csplit, 1)
{
const char* delimiters;
const char* content;
string backup1, backup2;
if (ARGC == 2)
{
delimiters = ARG(0)->toSZ(backup2);
content = ARG(1)->toSZ(backup1);
if (strlen(delimiters) == 0)
throw new ShhObjectException(9042, "first argument of split (delimiter) may not be empty");
}
else if (ARGC == 1)
{
delimiters = " \t\r\n\f";
content = ARG(0)->toSZ(backup1);
}
else
{
throw new ShhObjectException(9041, "function requres either one or two parameters");
}
array<string, true> items;
int itemCount = string::split(content, items, delimiters);
YYLTYPE ctorLocation;
ShhArray* a = (ShhArray*) module->createObject("ARRAY", ctorLocation);
a->setCount(itemCount);
for(int i=0; i<itemCount; i++)
{
string* item = items.get(i);
const char* sharedCopy = MAKE_STRING(item->cstr());
ShhValue element(sharedCopy);
a->set(i, element);
}
ShhValue result;
result.setArray(a);
return result;
}
SHORTHAND_BUILTIN(join, 2)
{
string bkDelimiter, bkElement;
string result;
const char* delimiter = ARG(0)->toSZ(bkDelimiter);
ShhValue* arg2 = ARG(1);
if (arg2->isArray())
{
ShhArray* a = arg2->toArray();
for(int i=0,n=a->size(); i<n; i++)
{
if (i>0) result.append(delimiter);
ShhValue element = a->get(i);
result.append(element.toSZ(bkElement));
}
}
else if (arg2->isHash())
{
ShhHash* h = arg2->toHash();
ShhHashIterator iterator;
int i=0;
h->createIterator(iterator);
ShhValue index, value;
while(iterator.next(index, value))
{
if (i>0) result.append(delimiter);
result.append(value.toSZ(bkElement));
i++;
}
}
else
{
throw new ShhObjectException(9042, "Second argument of join() must be array");
}
ShhValue rValue(result);
return rValue;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?