-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
String escaping #792
Comments
We can think this issue from other direction. Currently, static fields are not binded through FFI, instead they are translated as dart const static fields. That's why this issue arises. But with strings, we probably want a getter, because it's rare to use java string literal value in dart (by my experience). But when we pass such static string field to Java code, we need to convert it to Java string. So maybe it's better to leave it as a getter and not convert to direct literal. cc: @dcharkes . |
Good observation. Let's see what is most common in practise. If both are common, we can also generate both. (The Dart consts will be tree-shaken anyway in AOT.) |
One benefit of having them in Dart is easier reasoning about your code when writing code in Dart. E.g. doing a switch or equality check on something. When in the debugger you can see the string contents. |
We can generate the strings as raw strings like |
I am afraid it may not cover all cases. For example, java string contains escaped character like |
|
Has been a while since I dealt with that code. IIRC let's say you have java parser gives you string with actual newline character, that is { '\n' } you have got to escape this, either in java or in dart. I can't find an equivalent of say Go's There may be a way to get literal string on Java parser side, but I think I couldnt find one, or there were edge cases where Java escapes differently than dart which may be problematic again. My memory about this issue is a bit fuzzy. I think we even found a pub package offering this |
Here's an example. If you google the javadoc for excluded field, you get the string literal If you use single quotes to wrap strings, you will get same issue for a string So the plausible solutions are:
with trade offs discussed above. |
Let me see if I understand this correctly. So Java Parser parses the actual value of the String, for example Our task here is to undo this parsing method, which is to turn Now if we use something like To combat the problem with the different quotes we can split the string into multiple sections after each type of quote begins for example (turning
gets split into:
We can write the correct raw string for each one of the splits:
|
That would work for strings with quotes only, but literally every escaped character will be in its binary form. (consider non printable characters such as control+[a-z] characters, maybe even tab key). You will have to escape them because you can't store them reliably in text file. Second, will a raw string with complicated construction show up properly on hover / documentation? |
I see. I guess what I'm saying here is that what we currently have in place in code is a
For me it shows the exact definition, your milage may vary in your specific IDE. In general, I think we usually want to pass the |
So the verdict is to have the same
Regarding escaping of the literal, I think we should not take any shortcut, rather look at dart spec and write an escaping function. |
From discussion
|
Sidenote: we could turn all the non-alphanum characters into |
I think we can have a special "protected" constructor for
This is useful for multiple reasons:
@dcharkes What do you think? |
If performance is an issue, sgtm. |
It's not necessarily an issue. It's mainly useful for checking the content of The other performance benefits are merely an added bonus. We could even consider not filling up the cache for other situations to optimize memory usage (since the string would be stored both in Dart and Java.) |
Ah right. Sounds good. |
* Revert "[jnigen] Add `JLazyReference` and `JFinalString` (dart-archive/jnigen#400)" This reverts commit 8456a7c. * Close #792
The "perfect" solution here might look like Have a Expose static string fields as a composite structure class JniStringConstant {
String value; // computed at binding generation time
get JString jstring; // returns a jstring, method or getter
} Then in switch class you can use switch (value) {
case SomeClass.someConst.value: ...
} Forgive me for forgetting the dart syntax, it has been a while :) |
Currently we are pasting integer, float and String constants in java classes into dart classes directly. There are few edge cases though, where some non-escapable characters get included.
Escape string on java side: I should pull apache commons text for this one purpose, String escape doesn't seem to be in standard library.
Escape in dart: same story here. There's a strings package but that seems to be discontinued.
Ideally there should be something like https://pkg.go.dev/strconv#Quote
Otherwise should code this logic manually.
The text was updated successfully, but these errors were encountered: