Comment Illegal Unicode Sequences

点点圈 提交于 2019-12-12 13:11:43

问题


I was once working on a Java application dealing with unicode processing - and as usual to begin with, I write some code and test it, then comment out the working code and add some new lines., and this process goes on till I find the solution

The exact issue I had was commenting out illegal Unicode strings. Some unicode wasn't working and I wanted to just comment it out.. to my utter surprise, it wouldn't work.

Example Code:

class UnicodeTester{
//char someCharacter = "\ux13d";
}

javac UnicodeTester.java

UnicodeTester.java:2: illegal unicode escape
//char someCharacter = "\ux13d";
                              ^
1 error

Is there a way where I can comment out illegal unicode sequences?? I read the Java Language Specification 2.2 & 2.3, The Lexical Grammar is enforced before the Syntactic Grammar. PERIOD. But what is the most effective workaround other than removing them from the source code??


回答1:


You could work around it by inserting a space char after the backslash:

//char someCharacter = "\ ux13d";



回答2:


This sometimes happens when needing to embed something using PHP code into Javascript, such as an integer value, and need to comment some illegal value temporarily but PHP still gives error because the PHP code is run before the JS code is generated and as treated as comment.

Usually I do something like

class UnicodeTester{
//char someCharacter = "\\ux13d";  // illegal
}

so that I can still grep for \u later on or grep for illegal.



来源:https://stackoverflow.com/questions/3949130/comment-illegal-unicode-sequences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!