Comments (8)
I understand the default behavior. I could just convert a byte array into a string and pass it into the function to get around I guess.
You have already closed this issue, so I am not sure how much of a scope there is for discussion. I am also not sure what qualifies as a ‘serious reason’. The harfbuzz package uses ‘const char *’ in many places to store binary blob-like data. So a const char *
is actually more than just a string. So it seems a bit arbitrary to make a mandatory conversion to string. It is definitely a good default behavior, but it should be overridable. Otherwise, I will have to convert all binary data that I read from files into a string before I pass it to Harfbuzz. That seems very un-Go-like, and defeats the purpose of autogenerating the bindings.
from c-for-go.
@deepakjois I fully understand your concern, the problem is that it cannot be easily overridden at this moment, the fix requires some fundamental changes and I'm not ready to do this right now.
I closed this issue but I'll keep this discussion in mind. I want to collect more issues like that so it will be a critical mass that will drive the change or someone would find a potential problem with performance or unsafe access that really affects the sanity.
As for "Go-like" principle, the API consistency is far more important than the Go-likeness of types, because they never be like Go ones. Of course that's just my opinion, but I just letting you know why is this. Converting an immutable const char *
into a mutable type []byte
is the same thing as converting long long
into uint8
. These things cannot be "overriden" in manifest as well.
Otherwise, I will have to convert all binary data that I read from files into a string before I pass it to Harfbuzz
That's not that hard to do. You can include these functions into your generated package:
func unsafeString(b []byte) string {
hdr := (*sliceHeader)(unsafe.Pointer(&b))
return *(*string)(unsafe.Pointer(&stringHeader{
Data: hdr.Data,
Len: hdr.Len,
}))
}
func ReadData(r io.Reader) string {
buf, _ := io. ReadAll(r)
return unsafeString(buf)
}
func Data(b []byte) string {
return unsafeString(b)
}
from c-for-go.
Ok, sorry about this. It seems like it is working. But this is happening to me a lot. I think I make a change and it does not take effect until I delete all the generated code and run cgogen after that.
from c-for-go.
Ok sorry…no its not working. But I have more details about the problem. See replication here:
https://github.com/deepakjois/cgogentest/tree/3af0a09204b78c0e6d01bc3282593fe3459ecdb7
Basically, const char*
seem to get treated specially. If I change const char*
to char *
, then it works.
from c-for-go.
Yes, that has been done intentionally, from the Go spec:
A string value is a (possibly empty) sequence of bytes. Strings are immutable: once created, it is impossible to change the contents of a string. The predeclared string type is string.
That is exactly what const char*
is. There is no allocation passing that value into C.
I'll reopen the issue add tips for that only if there will be some serious reason. Thanks for caring.
from c-for-go.
Thanks for the clarification. It is good to hear that you are collecting the issues. Hopefully there is a good solution for this in the future.
As for your code, where does the *sliceHeader
and &stringHeader
come from?
from c-for-go.
They usually are put in cgo_helpers.go
when some string conversion occurs, so I expect that the generated package have it. Otherwise these are just renamed reflect.SliceHeader
and reflect.StringHeader
, I prefer to avoid including the reflect package and carry them around as private.
type stringHeader struct {
Data uintptr
Len int
}
type sliceHeader struct {
Data uintptr
Len int
Cap int
}
from c-for-go.
I have the same issue when trying to create go-bindings for libdivecomputer, it's an open source library for communication with dive computers from various manufacturers. And when it downloads the dive records from the dive computer it will pass the raw binary data to a callback as a const unsigned char *data
and when the generated bindings use packPUcharString
to convert this data into a string the resullt will be an empty string.
Here is an [mplementation of the callback in C: dctool_download.c
And here is an example how is used: shearwater_petrel.c
And I have to say that c-for-go is a really neat tool :)
from c-for-go.
Related Issues (20)
- How to handle a function returning a struct as value
- Type translation does not respect systems data model HOT 1
- Verbose argument to print more logs
- Avoid lock / unlock for some function call
- Wiki need to be updated
- panic: unknown type _Float128 HOT 2
- Non int return function pointer not recognized
- Generator uses underlying type of typedef when used as a parameter
- Website down HOT 3
- It would be nice to at least have an example of the YML required in the readme since the website does not work HOT 1
- build HOT 5
- [OS X / Ubuntu ARM64] include file not found: <string> HOT 3
- Extracting SAL annotations from declarations HOT 2
- Example where C const get translated into Go typed const
- Callback generation HOT 2
- THIS IS NOT AN ISSUE!!!
- Avoid symbol lookup error
- Passing in an array to a C function and reading the data - incorrect autogenerated Go code
- Write rules against the location of the type/define rather than the name
- Fails to parse vulkan_beta.h
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c-for-go.