Revert "removed bits C code inside utf8 builtin"

This reverts commit f6a401aa02.
pull/691/head
Alexander Medvednikov 2019-06-27 02:12:28 +02:00
parent bda50e96f0
commit b9011804fc
1 changed files with 63 additions and 53 deletions

View File

@ -251,63 +251,71 @@ fn (s string) runes() []string {
// Convert utf32 to utf8 // Convert utf32 to utf8
// utf32 == Codepoint // utf32 == Codepoint
fn utf32_to_str(code u32) string { fn utf32_to_str(code u32) string {
icode := int(code) //Prevents doing casts everywhere // println('code = $code')
mut buffer := malloc(5) buffer := malloc(5)
if icode <= 127 /* 0x7F */ { # if (code <= 0x7F) {
buffer[0] = icode // println('!!!!!!!1')
return tos(buffer, 1) # buffer[0] = code;
} # return tos(buffer, 1);
if (icode <= 2047 /* 0x7FF */) { # }
buffer[0] = 192 /*0xC0*/ | (icode >> 6) /* 110xxxxx */ # if (code <= 0x7FF) {
buffer[1] = 128 /*0x80*/ | (icode & 63 /*0x3F*/) /* 10xxxxxx */ // println('!!!!!!!2')
return tos(buffer, 2) # buffer[0] = 0xC0 | (code >> 6); /* 110xxxxx */
} # buffer[1] = 0x80 | (code & 0x3F); /* 10xxxxxx */
if (icode <= 65535 /* 0xFFFF */) { # return tos(buffer, 2);
buffer[0] = 224 /*0xE0*/ | (icode >> 12) /* 1110xxxx */ # }
buffer[1] = 128 /*0x80*/ | ((icode >> 6) & 63 /*0x3F*/) /* 10xxxxxx */ # if (code <= 0xFFFF) {
buffer[2] = 128 /*0x80*/ | (icode & 63 /*0x3F*/) /* 10xxxxxx */ // println('!!!!!!!3')
return tos(buffer, 3) # buffer[0] = 0xE0 | (code >> 12); /* 1110xxxx */
} # buffer[1] = 0x80 | ((code >> 6) & 0x3F); /* 10xxxxxx */
if (icode <= 1114111 /* 0x10FFFF */) { # buffer[2] = 0x80 | (code & 0x3F); /* 10xxxxxx */
buffer[0] = 240 /*0xF0*/ | (icode >> 18) /* 11110xxx */ # return tos(buffer, 3);
buffer[1] = 128 /*0x80*/ | ((icode >> 12) & 63 /*0x3F*/) /* 10xxxxxx */ # }
buffer[2] = 128 /*0x80*/ | ((icode >> 6) & 63 /*0x3F*/) /* 10xxxxxx */ # if (code <= 0x10FFFF) {
buffer[3] = 128 /*0x80*/ | (icode & 63 /*0x3F*/) /* 10xxxxxx */ # buffer[0] = 0xF0 | (code >> 18); /* 11110xxx */
return tos(buffer, 4) # buffer[1] = 0x80 | ((code >> 12) & 0x3F); /* 10xxxxxx */
} # buffer[2] = 0x80 | ((code >> 6) & 0x3F); /* 10xxxxxx */
# buffer[3] = 0x80 | (code & 0x3F); /* 10xxxxxx */
# return tos(buffer, 4);
# }
return '' return ''
} }
// TODO copypasta // TODO copypasta
fn utf32_to_str_no_malloc(code u32, buffer byteptr) string { fn utf32_to_str_no_malloc(code u32, buf voidptr) string {
icode := int(code) //Prevents doing casts everywhere // println('code = $code')
if icode <= 127 /* 0x7F */ { # char* buffer = buf;
buffer[0] = icode # if (code <= 0x7F) {
return tos(buffer, 1) // println('!!!!!!!1')
} # buffer[0] = code;
if (icode <= 2047 /* 0x7FF */) { # return tos(buffer, 1);
buffer[0] = 192 /*0xC0*/ | (icode >> 6) /* 110xxxxx */ # }
buffer[1] = 128 /*0x80*/ | (icode & 63 /*0x3F*/) /* 10xxxxxx */ # if (code <= 0x7FF) {
return tos(buffer, 2) // println('!!!!!!!2')
} # buffer[0] = 0xC0 | (code >> 6); /* 110xxxxx */
if (icode <= 65535 /* 0xFFFF */) { # buffer[1] = 0x80 | (code & 0x3F); /* 10xxxxxx */
buffer[0] = 224 /*0xE0*/ | (icode >> 12) /* 1110xxxx */ # return tos(buffer, 2);
buffer[1] = 128 /*0x80*/ | ((icode >> 6) & 63 /*0x3F*/) /* 10xxxxxx */ # }
buffer[2] = 128 /*0x80*/ | (icode & 63 /*0x3F*/) /* 10xxxxxx */ # if (code <= 0xFFFF) {
return tos(buffer, 3) // println('!!!!!!!3')
} # buffer[0] = 0xE0 | (code >> 12); /* 1110xxxx */
if (icode <= 1114111 /* 0x10FFFF */) { # buffer[1] = 0x80 | ((code >> 6) & 0x3F); /* 10xxxxxx */
buffer[0] = 240 /*0xF0*/ | (icode >> 18) /* 11110xxx */ # buffer[2] = 0x80 | (code & 0x3F); /* 10xxxxxx */
buffer[1] = 128 /*0x80*/ | ((icode >> 12) & 63 /*0x3F*/) /* 10xxxxxx */ # return tos(buffer, 3);
buffer[2] = 128 /*0x80*/ | ((icode >> 6) & 63 /*0x3F*/) /* 10xxxxxx */ # }
buffer[3] = 128 /*0x80*/ | (icode & 63 /*0x3F*/) /* 10xxxxxx */ # if (code <= 0x10FFFF) {
return tos(buffer, 4) # buffer[0] = 0xF0 | (code >> 18); /* 11110xxx */
} # buffer[1] = 0x80 | ((code >> 12) & 0x3F); /* 10xxxxxx */
# buffer[2] = 0x80 | ((code >> 6) & 0x3F); /* 10xxxxxx */
# buffer[3] = 0x80 | (code & 0x3F); /* 10xxxxxx */
# return tos(buffer, 4);
# }
return '' return ''
} }
// Convert utf8 to utf32 // Convert utf8 to utf32
fn (_rune string) utf32_code() int { fn (_rune string) utf32_code() int {
// println('utf 32 of $rune len=$rune.len')
if _rune.len == 0 { if _rune.len == 0 {
return 0 return 0
} }
@ -315,18 +323,20 @@ fn (_rune string) utf32_code() int {
if _rune.len == 1 { if _rune.len == 1 {
return int(_rune[0]) return int(_rune[0])
} }
mut b := byte(int(_rune[0])) b := byte(int(_rune[0]))
// TODO should be // TODO should be
// res := int( rune[0] << rune.len) // res := int( rune[0] << rune.len)
b <<= _rune.len # b <<= _rune.len;
mut res := int(b) res := int(b)
mut shift := 6 - _rune.len mut shift := 6 - _rune.len
for i := 1; i < _rune.len; i++ { for i := 1; i < _rune.len; i++ {
// println('c=$res')
c := int(_rune[i]) c := int(_rune[i])
res <<= shift # res <<= shift;
res |= c & 63 /* 0x3f */ # res |= c & 0x3f;
shift = 6 shift = 6
} }
// println('!!!!!!!! utf32 $rune res = $res')
return res return res
} }