Giter Club home page Giter Club logo

Comments (5)

rvagg avatar rvagg commented on August 22, 2024

What you're running in to I think is that v8::String::NO_NULL_TERMINATION is included in the default options so you're just overwriting it with an explicit v8::String::HINT_MANY_WRITES_EXPECTED. I end up doing something similar pretty much every time I use NanFromV8String, e.g.: https://github.com/rvagg/node-leveldown/blob/master/src/database.cc#L156

So this is a pain point for me too. We perhaps should either change the default or introduce an additional macro (with what name?) to generate proper null-terminated strings.

from nan.

kkoopa avatar kkoopa commented on August 22, 2024

The defaults were made to make it somewhat compatible with the StringBytes family of functions. You have to actually look at the sizes of returned buffers to know what to copy and what not to, as zero-termination is not used internally, sizes may change and the strings may contain null bytes. It should be easy to make a wrapper and call it NanCString or something.like that.

from nan.

rvagg avatar rvagg commented on August 22, 2024

yeah, NanCString is a good name, tho that leads to an unfortunate disparity in naming, perhaps NanFromV8String should be renamed too?

from nan.

ngryman avatar ngryman commented on August 22, 2024

I guess NanFromV8String could be renamed (or aliased for backward compatibility) to NanCString as it already returns a C string. You could then add more specialized macros, like: NanCNullTerminatedString or NanGetCString

Not directly related, but is it possible for you to add a C++ std::string version? I think it could be useful, especially when you are doing some string manipulation after.

Something like:

inline static std::string NanCppString(v8::Local<v8::Value> v8Str) {
    char* str = NanFromV8String(v8Str, Nan::UTF8, NULL, NULL, 0, v8::String::NO_OPTIONS);
    std::string cppStr(str);
    delete[] cstr;
    return cppStr;
}

Thanks!

from nan.

kkoopa avatar kkoopa commented on August 22, 2024

I guess we can close this now that everything but a std::string version has been adressed.

from nan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.