Visual Studio:字符串文字中的零宽度空格字符:检测和/或防止 [英] Visual Studio: Zero Width Space character in string literals: Detect and/or prevent

查看:30
本文介绍了Visual Studio:字符串文字中的零宽度空格字符:检测和/或防止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio 允许使用 unicode 字符

Visual Studio allows the unicode character Zero Width Space (U+200B) to be pasted into the code editor. This character is (as the name implies) invisible.

This can lead to nasty bugs such as the one I just spent time troubleshooting where HttpWebRequest.CreateHttp(string url) threw a UriFormatException when passed the innocuous-looking string literal "​http://www.umich.edu".

The exception occurred because the first character in that string literal is not h, as it appears, but is the Zero Width Space character. It was put onto the clipboard when I copied the URL from the body of a web page, and was dutifully pasted into my code by Visual Studio when I hit Ctrl+V in the code editor window.

Turning on the "View White Space" option (Edit > Advanced > View White Space) does not cause Visual Studio to reveal that there's a Zero Width Space character present.

I would like Visual Studio to:

  • Give some kind of indication or warning when there's a Zero Width Space character (or other invisible character) in a string literal in my code, and/or
  • Prevent such control characters from being pasted into the code editor in the first place.

Is there a way to make Visual Studio do this?

解决方案

The Gremlins tracker for Visual Studio Code worked for me.
https://github.com/nhoizey/vscode-gremlins

P.s.: IntelliJ recognizes those hidden troublesome chars OOTB

这篇关于Visual Studio:字符串文字中的零宽度空格字符:检测和/或防止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆