C++写node笔记(七)
by snoopyxdy
at 2013-01-25 14:49:07
original http://snoopyxdy.blog.163.com/blog/static/60117440201302431946486
#define BUILDING_NODE_EXTENSION#include <node.h>#include "xxx.h"using namespace v8;void Init(Handle<Object> target) {target->Set(String::NewSymbol("输出属性名"),FunctionTemplate::New(C++函数名)->GetFunction());}NODE_MODULE(输出模块名, Init)
#include <node.h>#include <string>class SimpleF {public:static v8::Handle<v8::Value> trim(const v8::Arguments& args);static v8::Handle<v8::Value> ltrim(const v8::Arguments& args);static v8::Handle<v8::Value> rtrim(const v8::Arguments& args);static v8::Handle<v8::Value> toXss(const v8::Arguments& args);static v8::Handle<v8::Value> replace(const v8::Arguments& args);protected:static void toCString(std::string& str, v8::Handle<v8::Value> strp);static void lTrimString(std::string& str,const std::string& filters = "\r\t\n ");private:SimpleF();~SimpleF();};
Handle<Value> SimpleV::isArray(const Arguments& args) {//是否是数组HandleScope scope;return scope.Close(Boolean::New(args[0]->IsArray()));}
void router::toCString(std::string& str, Handle<Value> strp){
String::Utf8Value utf8_value(strp->ToString());//转化成v8::Utf8Value
str = *utf8_value;//转化为string
}
std::string s;
const char *sp = s.c_str();
return scope.Close(String::New(sp));
void SimpleF::CSreplace(std::string& s1,std::string& s2,std::string& s3)
{
std::string::size_type pos=0;
std::string::size_type a=s2.size();
std::string::size_type b=s3.size();
while((pos=s1.find(s2,pos))!=std::string::npos)
{
s1.replace(pos,a,s3);
pos+=b;
}
}
linux正则匹配:#include <regex.h>
regex_t genRegex(std::string regstr){
regex_t preg;
const char *regex = regstr.c_str();
regcomp(&preg, regex, REG_EXTENDED|REG_NOSUB);
return preg;
}
bool match(regex_t preg,std::string& str){
int z;
const char *buf = str.c_str();
const size_t nmatch = 1;
regmatch_t pm[nmatch];
z = regexec(&preg, buf, nmatch, pm, 0);
return z != REG_NOMATCH;
}
windows下正则匹配:#include <regex>
std::regex genRegex(std::string regstr){
std::regex pattern(regstr,std::regex_constants::extended);
return pattern;
}
bool match(std::regex pattern,std::string& str){
std::match_results<std::string::const_iterator> result;
bool valid = std::regex_match(str,result,pattern);
return valid;
}
Local<Function> cb = Local<Function>::Cast(args[0]);
const unsigned argc = 1;
Local<Value> argv[argc] = { Local<Value>::New(String::New("hello world")) };
cb->Call(Context::GetCurrent()->Global(), argc, argv);