Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active January 19, 2023 02:06
Show Gist options
  • Select an option

  • Save ninmonkey/a0930e24eaabec8a6a3a31a5ea1fffb4 to your computer and use it in GitHub Desktop.

Select an option

Save ninmonkey/a0930e24eaabec8a6a3a31a5ea1fffb4 to your computer and use it in GitHub Desktop.
custom type data for [Text.Rune] and [String]s in Pwsh
$updateTypeDataSplat = @{
TypeName = 'System.String'
MemberName = 'LengthUnicode' # bad name on purpose
MemberType = 'ScriptProperty'
Value = { @($this.EnumerateRunes()).count }
}
Update-TypeData @updateTypeDataSplat -Force
$updateTypeDataSplat = @{
TypeName = 'System.Text.Rune'
MemberType = 'ScriptProperty'
MemberName = 'Render'
}
Update-TypeData @updateTypeDataSplat -Force -Value {
# coerce control chars to safe symbols
$isCtrlChar = $this.Value -ge 0 -and $this.Value -le 0x1f
$Rune = $isCtrlChar ? [Text.Rune]::New($this.Value + 0x2400 ) : $this
return $Rune.ToString() # Should I be explicitly calling?
}
$updateTypeDataSplat = @{
TypeName = 'System.Text.Rune'
MemberType = 'ScriptProperty'
MemberName = 'Hex'
Value = {
'0x{0:x}' -f @($This.Value)
}
}
Update-TypeData @updateTypeDataSplat -Force
# $updateTypeDataSplat = @{
# TypeName = 'System.Text.Rune'
# MemberType = 'ScriptProperty'
# MemberName = 'SummarizeRune'
# Value = {
# # this kind of breaks, duplicating output
# # I believe it's because it's referencing a [StringRuneEnumerator]
# $target = $this
# [pscustomobject]@{ Len = $target.Length
# LengthCodepoints = $Target.LengthUnicode
# Render = $target
# }
# }
# }
# Update-TypeData @updateTypeDataSplat -Force
$updateTypeDataSplat = @{
TypeName = 'System.Text.Rune'
MemberType = 'ScriptProperty'
MemberName = 'isCtrlChar'
Value = {
$This.ToString() -match '\p{C}'
}
}
Update-TypeData @updateTypeDataSplat -Force
$updateTypeDataSplat = @{
TypeName = 'System.Text.Rune'
MemberType = 'ScriptProperty'
MemberName = 'Is'
Value = {
$rune = $this
# $str = $_
[pscustomobject]@{
PSTypeName = 'Rune.IsA.unicodeClassesRecord.proto'
Ascii = $Rune.Value -le 0x1f
CtrlChar = $rune -match '\p{C}'
Letter = $rune -match '\p{L}'
# More = '...' # https://www.regular-expressions.info/unicode.html
Numeric = $Rune -match '\p{N}'
Punctuation = $Rune.Value -match '\p{P}'
Separator = $Rune -match '\p{Z}' # '\p{Separator}'
Symbol = $Rune -match '\p{S}' # \p{Symbol}
}
}
}
Update-TypeData @updateTypeDataSplat -Force
$updateTypeDataSplat = @{
TypeName = 'System.Text.Rune'
DefaultDisplayPropertySet = 'Render', 'Hex', 'IsAscii', 'IsCtrlChar', 'Utf16SequenceLength', 'Utf8SequenceLength', 'Value'
}
Update-TypeData @updateTypeDataSplat -Force

To try:

Ps7> @('asfs '.EnumerateRunes()).Is | ft

Ps7> @('a s▸·⇢⁞ ┐⇽▂fs '.EnumerateRunes()) | Select Rune, Render -ExpandProperty Is | ft

Ps7> 0..0xff | %{ [Text.Rune]$_ } | fw -AutoSize

    ␀  ␁  ␂ ␃ ␄ ␅ ␆ ␇ ␈ ␉ ␊ ␋ ␌ ␍ ␎ ␏ ␐ ␑ ␒ ␓ ␔ ␕ ␖ ␗ ␘ ␙ ␚ ␛ ␜ ␝ ␞ ␟   ! " # $ % & ' ( ) * +
    ,  -  . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W
    X  Y  Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
                                    ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ­ ® ¯
    °  ±  ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û
    Ü  Ý  Þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ


Ps7> @('a s▸·⇢⁞ ┐⇽▂fs '.EnumerateRunes()) | Select Rune, Render -ExpandProperty Is | ft

  Ascii CtrlChar Letter Numeric Punctuation Separator Symbol Rune Render
  ----- -------- ------ ------- ----------- --------- ------ ---- ------
  False    False   True   False       False     False  False      a
  False    False  False   False       False      True  False
  False    False   True   False       False     False  False      s
  False    False  False   False       False     False   True      ▸
  False    False  False   False       False     False  False      ·
  False    False  False   False       False     False   True      ⇢
  False    False  False   False       False     False  False      ⁞
  False    False  False   False       False      True  False
  False    False  False   False       False     False   True      ┐
  False    False  False   False       False     False   True      ⇽
  False    False  False   False       False     False   True      ▂
  False    False   True   False       False     False  False      f
  False    False   True   False       False     False  False      s
  False    False  False   False       False      True  False



'👩‍👩‍👧‍👦'.EnumerateRunes() | Ft

Ps7.3.1> '👩‍👩‍👧‍👦'.EnumerateRunes() | Ft

Render Hex     IsAscii IsCtrlChar Utf16SequenceLength Utf8SequenceLength  Value
------ ---     ------- ---------- ------------------- ------------------  -----
👩     0x1f469   False       True                   2                  4 128105
‍       0x200d    False       True                   1                  3   8205
👩     0x1f469   False       True                   2                  4 128105
‍      0x200d    False       True                   1                  3   8205
👧     0x1f467   False       True                   2                  4 128103
‍      0x200d    False       True                   1                  3   8205
👦     0x1f466   False       True                   2                  4 128102

Ps7.3.1> '👩‍👩‍👧‍👦'.EnumerateRunes() | Ft

Render Hex     IsAscii IsCtrlChar Utf16SequenceLength Utf8SequenceLength  Value
------ ---     ------- ---------- ------------------- ------------------  -----
👩     0x1f469   False       True                   2                  4 128105
‍      0x200d    False       True                   1                  3   8205
👩     0x1f469   False       True                   2                  4 128105
‍      0x200d    False       True                   1                  3   8205
👧     0x1f467   False       True                   2                  4 128103
‍      0x200d    False       True                   1                  3   8205
👦     0x1f466   False       True                   2                  4 128102
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment