473,786 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it safe to use UTF-8 in comments?

I am not familiar with the UTF-8 encoding, but I know that it encodes
certain characters with up to four bytes. Is it safe to use UTF-8
encoded comments in C++ source files? For example, is there a remote
possibility that some multi-byte character, when interpreted
byte-by-byte, will contain */ and close the comment? Or is there
something else that can go wrong?
Jun 8 '07 #1
9 2931
On 2007-06-08, Szabolcs <sz******@gmail .comwrote:
I am not familiar with the UTF-8 encoding, but I know that it encodes
certain characters with up to four bytes. Is it safe to use UTF-8
encoded comments in C++ source files? For example, is there a remote
possibility that some multi-byte character, when interpreted
byte-by-byte, will contain */ and close the comment? Or is there
something else that can go wrong?
Due to the nature of the UTF-8 encoding it is the case that no
byte of any multibyte character will ever match any byte that is a
valid single byte character. In particular no part of a multibyte
character could ever match the two byte, two character sequence "*/".

I'm personally in favour of keeping C++ source files to containing
only us-ascii characters so that compilers which treat source files as
iso8859-1, us-ascii, utf-8 and wider range of other character encoding
schemes will interpret the source files in exactly the same way.

Just in comments, it would be a picky compiler that would complain
about utf-8 (unless of course it was expecting some EBCDIC like, or
other non-ISO-636 invariant subset compatible encoding).
Jun 8 '07 #2
Szabolcs wrote:
I am not familiar with the UTF-8 encoding, but I know that it encodes
certain characters with up to four bytes. Is it safe to use UTF-8
encoded comments in C++ source files? For example, is there a remote
possibility that some multi-byte character, when interpreted
byte-by-byte, will contain */ and close the comment? Or is there
something else that can go wrong?
Your reservations are valid, but you can rest easy. Comments could
contain any characters, and they are mapped during the very first
phase of translation into characters from the basic character set.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #3
On 2007-06-08, Charles Bailey <us**********@h ashpling.orgwro te:
Just in comments, it would be a picky compiler that would complain
about utf-8 (unless of course it was expecting some EBCDIC like, or
other non-ISO-636 invariant subset compatible encoding).
That would of course be ISO-646.
Jun 8 '07 #4
Victor Bazarov wrote:
Szabolcs wrote:
>I am not familiar with the UTF-8 encoding, but I know that it encodes
certain characters with up to four bytes. Is it safe to use UTF-8
encoded comments in C++ source files? For example, is there a remote
possibility that some multi-byte character, when interpreted
byte-by-byte, will contain */ and close the comment? Or is there
something else that can go wrong?

Your reservations are valid, but you can rest easy. Comments could
contain any characters, and they are mapped during the very first
phase of translation into characters from the basic character set.

V
Thanks for all the replies!

Szabolcs
Jun 8 '07 #5
On Jun 8, 9:40 pm, Szabolcs <szhor...@gmail .comwrote:
I am not familiar with the UTF-8 encoding, but I know that it encodes
certain characters with up to four bytes. Is it safe to use UTF-8
encoded comments in C++ source files? For example, is there a remote
possibility that some multi-byte character, when interpreted
byte-by-byte, will contain */ and close the comment? Or is there
something else that can go wrong?
Formally: "Physical source file characters are mapped, in an
implementation-defined manner, to the basic source character set
(introducing new-line characters for end-of-line indicators) if
necessary." That's the very first thing that happens, and the
standard places no restrictions on how this occurs: an
implementation could legally just and each character with 0x7F,
for example. (Because this is "implementa tion defined", the
implementation is required to document what it does. Good luck
in finding such documentation.) And implementation could also
simply decide that they are illegal characters, that the source
file is corrupt, and throw it out.

In practice, on a machine using an ASCII compatible encoding for
the basic character set, there's probably no risk in comments.
UTF-8 was designed expressedly so that all bytes in a multibyte
sequence have the bit 7 set, and thus can never be mistaken for
a single byte character, and implementations just copy the bytes
of comments, until they encounter the end of line character or
the sequence "*/" (depending on the type of comment).

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 8 '07 #6
On Jun 8, 9:58 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Szabolcs wrote:
I am not familiar with the UTF-8 encoding, but I know that it encodes
certain characters with up to four bytes. Is it safe to use UTF-8
encoded comments in C++ source files? For example, is there a remote
possibility that some multi-byte character, when interpreted
byte-by-byte, will contain */ and close the comment? Or is there
something else that can go wrong?
Your reservations are valid, but you can rest easy. Comments could
contain any characters, and they are mapped during the very first
phase of translation into characters from the basic character set.
Yup. And the mapping can be anything the implementation wants.
Which theoretically, at least, is far from assuring.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 8 '07 #7
Charles Bailey wrote:
On 2007-06-08, Szabolcs <sz******@gmail .comwrote:
>I am not familiar with the UTF-8 encoding, but I know that it encodes
certain characters with up to four bytes. Is it safe to use UTF-8
encoded comments in C++ source files? For example, is there a remote
possibility that some multi-byte character, when interpreted
byte-by-byte, will contain */ and close the comment? Or is there
something else that can go wrong?

Due to the nature of the UTF-8 encoding it is the case that no
byte of any multibyte character will ever match any byte that is a
valid single byte character. In particular no part of a multibyte
character could ever match the two byte, two character sequence "*/".

I'm personally in favour of keeping C++ source files to containing
only us-ascii characters so that compilers which treat source files as
iso8859-1, us-ascii, utf-8 and wider range of other character encoding
schemes will interpret the source files in exactly the same way.

Just in comments, it would be a picky compiler that would complain
about utf-8 (unless of course it was expecting some EBCDIC like, or
other non-ISO-636 invariant subset compatible encoding).
I started with writing my code in UTF8, but when it try to include it
with listings in LaTeX I get an error when using characters æ,ø and å.
Maybe you don't use those chars but still I would stick to ISO.8859-1
which I am currently using instead.
Jun 12 '07 #8
desktop wrote:
Charles Bailey wrote:
>On 2007-06-08, Szabolcs <sz******@gmail .comwrote:
Just in comments, it would be a picky compiler that would complain
about utf-8 (unless of course it was expecting some EBCDIC like, or
other non-ISO-636 invariant subset compatible encoding).

I started with writing my code in UTF8, but when it try to include it
with listings in LaTeX I get an error when using characters æ,ø and å.
Generally, I use only English in source code. I both results in limited
needs for non-ASCII characters as well as readability for programmers not
speaking nordic languages, and no IMO ugly mix between languages.
Maybe you don't use those chars but still I would stick to ISO.8859-1
which I am currently using instead.
Which would also cause troubles with LaTeX if you're running it in another
charset. LaTeX also handles utf8 these days. The simple change from
\usepackage[latin1]{inputenc}
to
\usepackage[utf8]{inputenc}
works quite well on my computer. But this will result in problems with
latin1 files.

--
Robert Bauck Hamar

Jun 12 '07 #9
Robert Bauck Hamar wrote:
desktop wrote:
>Charles Bailey wrote:
>>On 2007-06-08, Szabolcs <sz******@gmail .comwrote:
Just in comments, it would be a picky compiler that would complain
about utf-8 (unless of course it was expecting some EBCDIC like, or
other non-ISO-636 invariant subset compatible encoding).
I started with writing my code in UTF8, but when it try to include it
with listings in LaTeX I get an error when using characters æ,ø and å.

Generally, I use only English in source code. I both results in limited
needs for non-ASCII characters as well as readability for programmers not
speaking nordic languages, and no IMO ugly mix between languages.
>Maybe you don't use those chars but still I would stick to ISO.8859-1
which I am currently using instead.

Which would also cause troubles with LaTeX if you're running it in another
charset. LaTeX also handles utf8 these days. The simple change from
\usepackage[latin1]{inputenc}
to
\usepackage[utf8]{inputenc}
works quite well on my computer. But this will result in problems with
latin1 files.
I have tried giving utf8 as option to inputenc and keep my sourcecode
and .tex file in utf8 but it gives an error when I include with listings.

It seems that others also have this problem:
http://groups.google.d k/group/linux.debian.bu gs.dist/browse_thread/thread/5ddfcc41e022ff7 6/4db764476c6b541 1?lnk=st&q=!+Pa ckage+inputenc+ Error%3A+Unicod e+char+&rnum=8& hl=da#4db764476 c6b5411

But it can partly be solved when setting extendedchars=f alse.
Jun 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4192
by: lawrence | last post by:
Someone on www.php.net suggested using a seems_utf8() method to test text for UTF-8 character encoding but didn't specify how to write such a method. Can anyone suggest a test that might work? Something that maybe gives 90% confidence that a given block of text is or is not UTF-8 encoded?
12
8231
by: Mike Dee | last post by:
A very very basic UTF-8 question that's driving me nuts: If I have this in the beginning of my Python script in Linux: #!/usr/bin/env python # -*- coding: UTF-8 -*- should I - or should I not - be able to use non-ASCII characters in strings and in Tk GUI button labels and GUI window titles and in raw_input data without Python returning wrong case in manipulated
38
5739
by: Haines Brown | last post by:
I'm having trouble finding the character entity for the French abbreviation for "number" (capital N followed by a small supercript o, period). My references are not listing it. Where would I find an answer to this question (don't find it in the W3C_char_entities document). -- Haines Brown brownh@hartford-hwp.com
10
2889
by: Thorsten Ottosen | last post by:
Hi, I'm trying to escape html before its saved in a database. I tried $text = htmlentities( $reader->value ); but that don't work for e.g. japanese characters.
6
13894
by: archana | last post by:
Hi all, can someone tell me difference between unicode and utf 8 or utf 18 and which one is supporting more character set. whic i should use to support character ucs-2. I want to use ucs-2 character in streamreader and streamwriter. How unicode and utf chacters are stored.
7
12153
by: Jimmy Shaw | last post by:
Hi everybody, Is there any SIMPLE way to convert from UTF-16 to UTF-32? I may be mixed up, but is it possible that all UTF-16 "code points" that are 16 bits long appear just the same in UTF-32, but with zero padding and hence no real conversion is necessary? If I am completely wrong and some intricate conversion operation needs to take place, can anyone give me some primer on the subject?
28
2828
by: Protoman | last post by:
I'd like to now if this smart pointer class if thread-safe: Code: // COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06 // ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED // I HAVE WORKED VERY HARD AND HAVE SPENT HOURS OF // TIME DEVELOPING THIS. DO NOT COPY IT OR I WILL SUE YOU // ************************************************************ #pragma once
10
19580
by: Jed | last post by:
I have a form that needs to handle international characters withing the UTF-8 character set. I have tried all the recommended strategies for getting utf-8 characters from form input to email message and I cannot get it to work. I need to stay with classic asp for this. Here are some things I tried: 'CDONTS Call msg.SetLocaleIDs(65001)
23
5030
by: Allan Ebdrup | last post by:
I hava an ajax web application where i hvae problems with UTF-8 encoding oc chineese chars. My Ajax webapplication runs in a HTML page that is UTF-8 Encoded. I copy and paste some chineese chars from another HTML page viewed in IE7, that is also UTF-8 encoded (search for "china" on google.com). I paste the chineese chars into a content editable div. My Ajax webservice compiles an XML where the data from the content editable div is...
2
1886
by: =?Utf-8?B?Umljaw==?= | last post by:
I am looking for some suggestions from the group on Source Safe. 1. What is the best practice/rule for the Source Safe to check-out and check-in files for a small group of developers? 2. What is the best practice/rule for the Source Safe to check-out and check-in files for a large group of developers?
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.