Postgres (v11) 將紅心 ?? 算作兩個字符,對于其他帶有選擇器單元的多位元組 UTF-8 字符,依此類推。有人知道我如何讓 postgres 計算真正的字符而不是位元組嗎?
例如,我希望下面的兩個例子都應該回傳 1。
選擇長度('??') = 2 (Unicode: 2764 FE0F)
選擇長度('???♂?') = 4 (Unicode: 1F3C3 200D 2642 FE0F)
更新
感謝人們指出 postgres 正確計算了 Unicode 代碼點以及為什么以及如何發生這種情況。
除了在 Python 或類似的官方 Unicode 字符位元組表中將表情符號字串預處理為位元組以獲得感知長度之外,我沒有看到任何其他選項。
uj5u.com熱心網友回復:
因此,執行此操作的一種方法是忽略變體選擇器中的所有字符,如果您達到通用標點范圍,則減少 2。
這可以轉換為 postgres 函式。
Python
"""
# For reference, these code pages apply to emojis
Name Range
Emoticons 1F600-1F64F
Supplemental_Symbols_and_Pictographs 1F900-1F9FF
Miscellaneous Symbols and Pictographs 1F300-1F5FF
General Punctuation 2000-206F
Miscellaneous Symbols 2600-26FF
Variation Selectors FE00-FE0F
Dingbats 2700-27BF
Transport and Map Symbols 1F680-1F6FF
Enclosed Alphanumeric Supplement 1F100-1F1FF
"""
emojis="???♂????♂????♂????♂????♂????♂????♂?" # true count is 7, postgres length() returns 28
true_count=0
for char in emojis:
d=ord(char)
char_type=None
if (d>=0x2000 and d<=0x206F) : char_type="GP" # Zero Width Joiner
elif (d>=0xFE00 and d<=0xFE0F) : char_type="VS" # Variation Selector
print(d, char_type)
if ( char_type=="GP") : true_count-=2
elif (char_type!="VS" ): true_count =1
print(true_count)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/323651.html
標籤:PostgreSQL utf-8 字符编码
上一篇:帶有嵌套查詢和UUID陣列欄位的HQL/JPQL查詢
下一篇:如何從文本中提取測量和/或單位?
