Converts an integer in the range 0~1112060 into a rune. Based on the ranges table in https://en.wikipedia.org/wiki/UTF-8
(i uint32)
| 120 | // Converts an integer in the range 0~1112060 into a rune. |
| 121 | // Based on the ranges table in https://en.wikipedia.org/wiki/UTF-8 |
| 122 | func intToRune(i uint32) rune { |
| 123 | if i < (1 << ONE_BYTE_BITS) { |
| 124 | return rune(i) |
| 125 | } |
| 126 | |
| 127 | if i < (1 << TWO_BYTE_BITS) { |
| 128 | r, size := utf8.DecodeRune([]byte{0b11000000 | getBits(i, 5, 6), 0b10000000 | getBits(i, 6, 0)}) |
| 129 | if size != 2 || r == utf8.RuneError { |
| 130 | panic(fmt.Sprintf("Error encoding an int %d with size 2, got rune %v and size %d", size, r, i)) |
| 131 | } |
| 132 | return r |
| 133 | } |
| 134 | |
| 135 | // Last -3 here needed because for some reason 3rd to last codepoint 65533 in this range |
| 136 | // was returning utf8.RuneError during encoding. |
| 137 | if i < ((1 << THREE_BYTE_BITS) - UNICODE_INVALID_RANGE_DELTA - 3) { |
| 138 | if i >= UNICODE_INVALID_RANGE_START { |
| 139 | i += UNICODE_INVALID_RANGE_DELTA |
| 140 | } |
| 141 | |
| 142 | r, size := utf8.DecodeRune([]byte{0b11100000 | getBits(i, 4, 12), 0b10000000 | getBits(i, 6, 6), 0b10000000 | getBits(i, 6, 0)}) |
| 143 | if size != 3 || r == utf8.RuneError { |
| 144 | panic(fmt.Sprintf("Error encoding an int %d with size 3, got rune %v and size %d", size, r, i)) |
| 145 | } |
| 146 | return r |
| 147 | } |
| 148 | |
| 149 | if i < (1<<FOUR_BYTE_BITS - UNICODE_INVALID_RANGE_DELTA - 3) { |
| 150 | i += UNICODE_INVALID_RANGE_DELTA + 3 |
| 151 | r, size := utf8.DecodeRune([]byte{0b11110000 | getBits(i, 3, 18), 0b10000000 | getBits(i, 6, 12), 0b10000000 | getBits(i, 6, 6), 0b10000000 | getBits(i, 6, 0)}) |
| 152 | if size != 4 || r == utf8.RuneError { |
| 153 | panic(fmt.Sprintf("Error encoding an int %d with size 4, got rune %v and size %d", size, r, i)) |
| 154 | } |
| 155 | return r |
| 156 | } |
| 157 | panic(fmt.Sprintf("The integer %d is too large for runeToInt()", i)) |
| 158 | } |
| 159 | |
| 160 | // Converts a rune generated by intToRune back to an integer |
| 161 | func runeToInt(r rune) uint32 { |
searching dependent graphs…