源代码是否应该以 UTF-8 格式保存 [英] Should source code be saved in UTF-8 format

查看:27
本文介绍了源代码是否应该以 UTF-8 格式保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以 UTF-8 格式保存源代码有多重要?

How important is it to save your source code in UTF-8 format?

Windows 上的 Eclipse 默认使用 CP1252 字符编码.CP1251 格式意味着可以保存非 UTF-8 字符,如果您从 Word 文档复制并粘贴以进行评论,我已经看到这种情况.

Eclipse on Windows uses CP1252 character encoding by default. The CP1251 format means non UTF-8 characters can be saved and I have seen this happen if you copy and paste from a Word document for a comment.

我问的原因是出于习惯,我将 Maven 编码设置为 UTF-8 格式,最近它发现了一些不可映射的错误.

The reason I ask is because out of habit I set-up Maven encoding to be in UTF-8 format and recently it has caught a few non mappable errors.

(更新)请添加这样做的任何原因和原因,是否有一些应该知道的常见问题?

(update) Please add any reasons for doing so and why, are there some common gotchas that should be known?

(更新)你的目标是什么?为了找到最佳实践,所以当问到我们为什么要使用 UTF-8 时,我有一个很好的答案,现在我没有.

(update) What is your goal? To find the best practice so when ask why should we use UTF-8 I have a good answer, right now I don't.

推荐答案

您的目标是什么?平衡您的需求与此选择的利弊.

What is your goal? Balance your needs against the pros and cons of this choice.

UTF-8 的优点

  • 允许使用所有字符文字而无需 uHHHH 转义

UTF-8 的缺点

  • 使用没有 uHHHH 的非 ASCII 字符会增加字符损坏的风险
    • 可能会出现字体和键盘问题
    • 需要在所有工具(编辑器、编译器构建脚本、差异工具)中记录并强制使用 UTF-8
    • using non-ASCII character literals without uHHHH increases risk of character corruption
      • font and keyboard issues can arise
      • need to document and enforce use of UTF-8 in all tools (editors, compilers build scripts, diff tools)

      ASCII 优点

      • 字符/字节映射被多种编码共享
        • 使源文件非常便携
        • 通常不需要指定编码元数据(因为如果将文件重新编码为 UTF-8、Windows-1252、ISO 8859-1 以及大多数缺少 UTF-16 和/或EBCDIC)

        ASCII 缺点

        • 有限的字符集
        • 这不是 1960 年代

        注意:ASCII 是 7 位的,不是扩展"的,不要与 Windows-1252、ISO 8859-1 或其他任何东西混淆.

        这篇关于源代码是否应该以 UTF-8 格式保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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