Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rwVw8-002Hdt-6k for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Apr 2024 23:40:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rwVw6-00GnIM-Sm for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Apr 2024 23:40:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rwVw6-00GnIE-Fw for pgsql-hackers@lists.postgresql.org; Mon, 15 Apr 2024 23:40:30 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rwVw3-001A4o-M9 for pgsql-hackers@postgresql.org; Mon, 15 Apr 2024 23:40:29 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1e0bec01232so31110935ad.3 for ; Mon, 15 Apr 2024 16:40:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1713224425; x=1713829225; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:date:to:from :subject:message-id:from:to:cc:subject:date:message-id:reply-to; bh=WFI2sC61gzEHiiPE4IOBIjsevy8qFkX8EHwu5rvLPgE=; b=ig4G+Xp1/EfC8OjZiaWkuDCjW8jdo0ne5hlkbeNgNFbxbq/FqT+6TrgiMi4ZYZzFZx qNXH489G3n8Wr4K4NaahZ0I5h48vC/1DaD8lwBvbWYfQljS+OahGx6aM4rDhTK2soI+n NJEu7upIWVQ0bRL2EJsKb3EIrDzLiNyego0+Tif3M6iSwqQ482A1zv2fY526tQ9PY2/P mJfA3ojCIikU/3TegIaQ9HneIXWrhIgZarfqh4u91+mL3KoVMlKJBhvvNnvycFcN+XmT p4V0hYYCvthl8pJPa38XOVmIBjcZ5ocLSqt3szLTIWNqqzzWy9miCIDUHTL1Ga9TLNRx adDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713224425; x=1713829225; h=mime-version:user-agent:content-transfer-encoding:date:to:from :subject:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=WFI2sC61gzEHiiPE4IOBIjsevy8qFkX8EHwu5rvLPgE=; b=OZHLC5mM5qBRioNh3OBkn4LrL70j8uPvL1AopILaaZ2Brv70MPY0Et29/qd+mGjWZZ fezFbM+peJK8/ItpwxI4O6PL27BjVGNxs1A0AP6NXhPOps0XDZFTUvR8MqQEryjBKBy8 I7gNgneh6LiRDINbKJOBNjUXp1VNHfhuv+mbYztjAvh0WDBJAMaypWdMnKsOPGDHj8V8 HrVZycb3JPPsqycwYi8VKu4ImAywmuKh+PjmMJMmcd3pz07aLQHKptgz5S3q+HKJzO9M FNYJdfTA7hNCuH0S/5fwo+jYsUatwwigWP4CCTGZBiQFitbP5onmagbXT8rKLL3NxqOn c7yA== X-Gm-Message-State: AOJu0YxRwwguRTEs/3aRUC2K6/idFN7rHbBLtRmPNAcTVa966FoTvu9P x1ThuE04iGS2zMAjM5d0WvZQb+NaINN8mwMElgl85eonGK5GAM9i02ayLdk4ysfpd60HNppP9HE = X-Google-Smtp-Source: AGHT+IHEIE1rZ2MH4ZFcJappA4dzCFhPqinpljFTbbbYuj4Pv/emVMQ0C8Lz4JzOi4nSVKIrZipOBA== X-Received: by 2002:a17:902:8c97:b0:1e4:2765:6343 with SMTP id t23-20020a1709028c9700b001e427656343mr8990914plo.62.1713224425258; Mon, 15 Apr 2024 16:40:25 -0700 (PDT) Received: from jeff-laptop.lan (c-76-102-242-158.hsd1.ca.comcast.net. [76.102.242.158]) by smtp.gmail.com with ESMTPSA id m7-20020a1709026bc700b001dd69aca213sm8456668plt.270.2024.04.15.16.40.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 16:40:24 -0700 (PDT) Message-ID: <3f2a117fd346c19bdb388e0fc3925873a1728bf6.camel@j-davis.com> Subject: [18] clarify the difference between pg_wchar, wchar_t, and Unicode code points From: Jeff Davis To: pgsql-hackers@postgresql.org Date: Mon, 15 Apr 2024 16:40:23 -0700 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.44.4-0ubuntu2 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I'm not sure I understand all of the history behind pg_wchar, but it seems to be some blend of: (a) Postgres's own internal representation of a decoded character (b) libc's wchar_t (c) Unicode code point For example, Postgres has its own encoding/decoding routines, so (a) is the most obvious definition. When the server encoding is UTF-8, the internal representation is a Unicode code point, which is convenient for the builtin and ICU providers, as well as some (most? all?) libc implementations. Other encodings have different represenations which seem to favor the libc provider. pg_wchar is also passed directly to libc routines like iswalpha_l() (see pg_wc_isalpha()), which is depending on definition (b). We guard it with: if (sizeof(wchar_t) >=3D 4 || c <=3D (pg_wchar) 0xFFFF) to ensure that the pg_wchar is representable in the libc's wchar_t type. As far as I can tell this is still no guarantee of correctness; it's just a sanity check. I didn't find an obviously better way of doing it, however. When using ICU, we also pass a pg_wchar directly to ICU routines, which depends on definition (c), and can lead to problems like: https://www.postgresql.org/message-id/e7b67d24288f811aebada7c33f9ae629dde0d= ef5.camel@j-davis.com The comment at the top of pg_regc_locale.c explains some of the above, but not all. I'd like to organize this a bit better: * a new typedef for a Unicode code point ("codepoint"? "uchar"?) * a no-op conversion routine from pg_wchar to a codepoint that would assert that the server encoding is UTF-8 (#ifndef FRONTEND, of course) * a no-op conversion routine from pg_wchar to wchar_t that would be a good place for a comment describing that it's a "best effort" and may not be correct in all cases We could even go so far as to make the pg_wchar type not implicitly- castable, so that callers would be forced to convert it to either a wchar_t or a code point. Tom also suggested here: https://www.postgresql.org/message-id/360857.1701302164%40sss.pgh.pa.us that we don't necessarily need to use libc at all, and I like that idea. Perhaps the suggestions above are a step in that direction, or perhaps we can skip ahead? I intend to submit a patch for the July CF. Thoughts? Regards, Jeff Davis