Giter VIP home page Giter VIP logo

Comments (11)

jimfoltz avatar jimfoltz commented on August 10, 2024

Although maybe it's more correct to "chomp" the returned vector.

from sketchup-api-c-wrapper.

TommyKaneko avatar TommyKaneko commented on August 10, 2024

No, as long as it works without the extra character, we can get rid of it. Please test and make a pull request.

from sketchup-api-c-wrapper.

thomthom avatar thomthom commented on August 10, 2024

That vector or char is fed to SUStringGetUTF8 which expects char*. Further more the documentation describe:

This function does not allocate memory. You must provide an array of sufficient length to get the entire string. The output string will be NULL terminated.

The +1 to the vector size is to ensure enough memory is allocated.

std::string is then receiving a NULL terminated char* - which is standard way to represent a string in C. It knows to stop at NULL. No need to "chomp".

Unless anyone is seeing incorrect string lengths returned? (That would be a good case for setting up in a test unit.)

from sketchup-api-c-wrapper.

jimfoltz avatar jimfoltz commented on August 10, 2024

I am seeing an extra char - that's why I brought it up.

from sketchup-api-c-wrapper.

jimfoltz avatar jimfoltz commented on August 10, 2024

It may bot be the null terminator, but I do get some extra thing at the end of a string. This image is from windows command line using less.exe pager. So some maybe binary thing there - no time now to investigate.

00513

from sketchup-api-c-wrapper.

thomthom avatar thomthom commented on August 10, 2024

hmm...

That would be junk uninitialised data. Might be the creation of the vector;

std::vector<char> char_array(out_length);

Does it change if it's all initialized to null?

std::vector<char> char_array(out_length, 0);

Let me use that as a sample for the unit tests I'm scaffolding.

from sketchup-api-c-wrapper.

jimfoltz avatar jimfoltz commented on August 10, 2024

BTW the GetString() function you postyed has the same issue.

from sketchup-api-c-wrapper.

thomthom avatar thomthom commented on August 10, 2024

I'm seeing that the std::string's size is one larger and contain a NULL byte:

image

I'll investigate closer.

from sketchup-api-c-wrapper.

thomthom avatar thomthom commented on August 10, 2024

Ah, it was this line:

std::string str(char_array.begin(),char_array.end());

That copies all the bytes, including NULL bytes.

Replacing it with std::string str(char_array.data()); made it scan and stop at the NULL terminator.

However, that prevents String from representing binary data.

Maybe it's better to keep using begin() and end() but omit trying to read the extra NULL termination.

from sketchup-api-c-wrapper.

thomthom avatar thomthom commented on August 10, 2024

On second though, SUStringSetUTF8 relies on NULL termination, so there is no way it can represent binary data.

I'll switch to .data() and include it in my test unit PR.

from sketchup-api-c-wrapper.

thomthom avatar thomthom commented on August 10, 2024

Btw Jim, those weird characters were probably a result of Release mode uninitialized char in the vector. In Debug it looks to init to 0 - but Debug allocate memory differently.

from sketchup-api-c-wrapper.

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.