Skip to content

Commit 67c7ed4

Browse files
author
Ben Hoyt
committed
Fix conversion of byte to string
1 parent de11d13 commit 67c7ed4

File tree

6 files changed

+47
-36
lines changed

6 files changed

+47
-36
lines changed

converted/editor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func EditorLoop() {
9292
VideoWriteText(61+i, 22, byte(i), "\xdb")
9393
}
9494
for i = 1; i <= EditorPatternCount; i++ {
95-
VideoWriteText(61+i, 22, 0x0F, string(ElementDefs[EditorPatterns[i-1]].Character))
95+
VideoWriteText(61+i, 22, 0x0F, string([]byte{ElementDefs[EditorPatterns[i-1]].Character}))
9696
}
9797
if ElementDefs[copiedTile.Element].HasDrawProc {
9898
ElementDefs[copiedTile.Element].DrawProc(copiedX, copiedY, &copiedChr)
@@ -743,7 +743,7 @@ func EditorLoop() {
743743
elemMenuColor = int16(ElementDefs[iElem].Color)
744744
}
745745

746-
VideoWriteText(78, i, byte(elemMenuColor), string(ElementDefs[iElem].Character))
746+
VideoWriteText(78, i, byte(elemMenuColor), string([]byte{ElementDefs[iElem].Character}))
747747
i++
748748
}
749749
}

converted/game.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func WorldCreate() {
256256
func TransitionDrawToFill(chr byte, color int16) {
257257
var i int16
258258
for i = 1; i <= TransitionTableSize; i++ {
259-
VideoWriteText(TransitionTable[i-1].X-1, TransitionTable[i-1].Y-1, byte(color), string(chr))
259+
VideoWriteText(TransitionTable[i-1].X-1, TransitionTable[i-1].Y-1, byte(color), string([]byte{chr}))
260260
}
261261
}
262262

@@ -270,7 +270,7 @@ func BoardDrawTile(x, y int16) {
270270
ElementDefs[tile.Element].DrawProc(x, y, &ch)
271271
VideoWriteText(x-1, y-1, tile.Color, Chr(ch))
272272
} else if tile.Element < E_TEXT_MIN {
273-
VideoWriteText(x-1, y-1, tile.Color, string(ElementDefs[tile.Element].Character))
273+
VideoWriteText(x-1, y-1, tile.Color, string([]byte{ElementDefs[tile.Element].Character}))
274274
} else {
275275
if tile.Element == E_TEXT_WHITE {
276276
VideoWriteText(x-1, y-1, 0x0F, Chr(Board.Tiles[x][y].Color))
@@ -464,13 +464,13 @@ func PromptString(x, y, arrowColor, color, width int16, mode byte, buffer *strin
464464
switch mode {
465465
case PROMPT_NUMERIC:
466466
if InputKeyPressed >= '0' && InputKeyPressed <= '9' {
467-
*buffer += string(InputKeyPressed)
467+
*buffer += string([]byte{InputKeyPressed})
468468
}
469469
case PROMPT_ANY:
470-
*buffer += string(InputKeyPressed)
470+
*buffer += string([]byte{InputKeyPressed})
471471
case PROMPT_ALPHANUM:
472472
if UpCase(InputKeyPressed) >= 'A' && UpCase(InputKeyPressed) <= 'Z' || InputKeyPressed >= '0' && InputKeyPressed <= '9' || InputKeyPressed == '-' {
473-
*buffer += string(UpCase(InputKeyPressed))
473+
*buffer += string([]byte{UpCase(InputKeyPressed)})
474474
}
475475
}
476476
} else if InputKeyPressed == KEY_LEFT || InputKeyPressed == KEY_BACKSPACE {
@@ -744,7 +744,7 @@ func CopyStatDataToTextWindow(statId int16, state *TTextWindowState) {
744744
TextWindowAppend(state, dataStr)
745745
dataStr = ""
746746
} else {
747-
dataStr += string(dataChr)
747+
dataStr += string([]byte{dataChr})
748748
}
749749
AdvancePointer(&dataPtr, 1)
750750
}
@@ -983,7 +983,7 @@ func GameUpdateSidebar() {
983983
}
984984
for i = 1; i <= 7; i++ {
985985
if World.Info.Keys[i-1] {
986-
VideoWriteText(71+i, 12, byte(0x18+i), string(ElementDefs[E_KEY].Character))
986+
VideoWriteText(71+i, 12, byte(0x18+i), string([]byte{ElementDefs[E_KEY].Character}))
987987
} else {
988988
VideoWriteText(71+i, 12, 0x1F, " ")
989989
}
@@ -1184,7 +1184,7 @@ func GameDebugPrompt() {
11841184
SidebarClearLine(5)
11851185
PromptString(63, 5, 0x1E, 0x0F, 11, PROMPT_ANY, &input)
11861186
for i = 1; i <= Length(input); i++ {
1187-
input[i-1] = string(UpCase(input[i-1]))
1187+
input[i-1] = string([]byte{UpCase(input[i-1])})
11881188
}
11891189
toggle = true
11901190
if input[0] == '+' || input[0] == '-' {
@@ -1254,11 +1254,11 @@ func GamePlayLoop(boardChanged bool) {
12541254
VideoWriteText(64, 10, 0x1E, " Gems:")
12551255
VideoWriteText(64, 11, 0x1E, " Score:")
12561256
VideoWriteText(64, 12, 0x1E, " Keys:")
1257-
VideoWriteText(62, 7, 0x1F, string(ElementDefs[E_PLAYER].Character))
1258-
VideoWriteText(62, 8, 0x1B, string(ElementDefs[E_AMMO].Character))
1259-
VideoWriteText(62, 9, 0x16, string(ElementDefs[E_TORCH].Character))
1260-
VideoWriteText(62, 10, 0x1B, string(ElementDefs[E_GEM].Character))
1261-
VideoWriteText(62, 12, 0x1F, string(ElementDefs[E_KEY].Character))
1257+
VideoWriteText(62, 7, 0x1F, string([]byte{ElementDefs[E_PLAYER].Character}))
1258+
VideoWriteText(62, 8, 0x1B, string([]byte{ElementDefs[E_AMMO].Character}))
1259+
VideoWriteText(62, 9, 0x16, string([]byte{ElementDefs[E_TORCH].Character}))
1260+
VideoWriteText(62, 10, 0x1B, string([]byte{ElementDefs[E_GEM].Character}))
1261+
VideoWriteText(62, 12, 0x1F, string([]byte{ElementDefs[E_KEY].Character}))
12621262
VideoWriteText(62, 14, 0x70, " T ")
12631263
VideoWriteText(65, 14, 0x1F, " Torch")
12641264
VideoWriteText(62, 15, 0x30, " B ")
@@ -1337,7 +1337,7 @@ func GamePlayLoop(boardChanged bool) {
13371337
pauseBlink = !pauseBlink
13381338
}
13391339
if pauseBlink {
1340-
VideoWriteText(int16(Board.Stats[0].X)-1, int16(Board.Stats[0].Y)-1, ElementDefs[E_PLAYER].Color, string(ElementDefs[E_PLAYER].Character))
1340+
VideoWriteText(int16(Board.Stats[0].X)-1, int16(Board.Stats[0].Y)-1, ElementDefs[E_PLAYER].Color, string([]byte{ElementDefs[E_PLAYER].Character}))
13411341
} else {
13421342
if Board.Tiles[Board.Stats[0].X][Board.Stats[0].Y].Element == E_PLAYER {
13431343
VideoWriteText(int16(Board.Stats[0].X)-1, int16(Board.Stats[0].Y)-1, 0x0F, " ")

converted/input.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func InputUpdate() {
179179
if InputKeyPressed == '\x00' || InputKeyPressed == '\x01' || InputKeyPressed == '\x02' {
180180
InputKeyBuffer += Chr(Ord(ReadKey()) | 0x80)
181181
} else {
182-
InputKeyBuffer += string(InputKeyPressed)
182+
InputKeyBuffer += string([]byte{InputKeyPressed})
183183
}
184184
}
185185
if Length(InputKeyBuffer) != 0 {

converted/oop.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func OopReadWord(statId int16, position *int16) {
3232
OopChar = UpCase(OopChar)
3333
if OopChar < '0' || OopChar > '9' {
3434
for OopChar >= 'A' && OopChar <= 'Z' || OopChar == ':' || OopChar >= '0' && OopChar <= '9' || OopChar == '_' {
35-
OopWord += string(OopChar)
35+
OopWord += string([]byte{OopChar})
3636
OopReadChar(statId, position)
3737
OopChar = UpCase(OopChar)
3838
}
@@ -56,7 +56,7 @@ func OopReadValue(statId int16, position *int16) {
5656
}
5757
OopChar = UpCase(OopChar)
5858
for OopChar >= '0' && OopChar <= '9' {
59-
s += string(OopChar)
59+
s += string([]byte{OopChar})
6060
OopReadChar(statId, position)
6161
OopChar = UpCase(OopChar)
6262
}
@@ -306,7 +306,7 @@ func OopStringToWord(input string) (OopStringToWord string) {
306306
output = ""
307307
for i = 1; i <= Length(input); i++ {
308308
if input[i-1] >= 'A' && input[i-1] <= 'Z' || input[i-1] >= '0' && input[i-1] <= '9' {
309-
output += string(input[i-1])
309+
output += string([]byte{input[i-1]})
310310
} else if input[i-1] >= 'a' && input[i-1] <= 'z' {
311311
output += Chr(Ord(input[i-1]) - 0x20)
312312
}
@@ -443,7 +443,7 @@ func OopReadLineToEnd(statId int16, position *int16) (OopReadLineToEnd string) {
443443
s = ""
444444
OopReadChar(statId, position)
445445
for OopChar != '\x00' && OopChar != '\r' {
446-
s += string(OopChar)
446+
s += string([]byte{OopChar})
447447
OopReadChar(statId, position)
448448
}
449449
OopReadLineToEnd = s

converted/txtwind.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var (
4848
func UpCaseString(input string) (UpCaseString string) {
4949
var i int16
5050
for i = 1; i <= Length(input); i++ {
51-
input[i-1] = string(UpCase(input[i-1]))
51+
input[i-1] = string([]byte{UpCase(input[i-1])})
5252
}
5353
UpCaseString = input
5454
return
@@ -390,7 +390,7 @@ func TextWindowEdit(state *TTextWindowState) {
390390
}
391391
case KEY_BACKSPACE:
392392
if charPos > 1 {
393-
*state.Lines[state.LinePos-1] = string(Copy(*state.Lines[state.LinePos-1], 1, charPos-2) + Copy(*state.Lines[state.LinePos-1], charPos, Length(*state.Lines[state.LinePos-1])-charPos+1))
393+
*state.Lines[state.LinePos-1] = Copy(*state.Lines[state.LinePos-1], 1, charPos-2) + Copy(*state.Lines[state.LinePos-1], charPos, Length(*state.Lines[state.LinePos-1])-charPos+1)
394394
charPos--
395395
} else if Length(*state.Lines[state.LinePos-1]) == 0 {
396396
DeleteCurrLine()
@@ -401,17 +401,17 @@ func TextWindowEdit(state *TTextWindowState) {
401401
case KEY_INSERT:
402402
insertMode = !insertMode
403403
case KEY_DELETE:
404-
*state.Lines[state.LinePos-1] = string(Copy(*state.Lines[state.LinePos-1], 1, charPos-1) + Copy(*state.Lines[state.LinePos-1], charPos+1, Length(*state.Lines[state.LinePos-1])-charPos))
404+
*state.Lines[state.LinePos-1] = Copy(*state.Lines[state.LinePos-1], 1, charPos-1) + Copy(*state.Lines[state.LinePos-1], charPos+1, Length(*state.Lines[state.LinePos-1])-charPos)
405405
case KEY_CTRL_Y:
406406
DeleteCurrLine()
407407
default:
408408
if InputKeyPressed >= ' ' && charPos < TextWindowWidth-7 {
409409
if !insertMode {
410-
*state.Lines[state.LinePos-1] = string(Copy(*state.Lines[state.LinePos-1], 1, charPos-1) + InputKeyPressed + Copy(*state.Lines[state.LinePos-1], charPos+1, Length(*state.Lines[state.LinePos-1])-charPos))
410+
*state.Lines[state.LinePos-1] = Copy(*state.Lines[state.LinePos-1], 1, charPos-1) + InputKeyPressed + Copy(*state.Lines[state.LinePos-1], charPos+1, Length(*state.Lines[state.LinePos-1])-charPos)
411411
charPos++
412412
} else {
413413
if Length(*state.Lines[state.LinePos-1]) < TextWindowWidth-8 {
414-
*state.Lines[state.LinePos-1] = string(Copy(*state.Lines[state.LinePos-1], 1, charPos-1) + InputKeyPressed + Copy(*state.Lines[state.LinePos-1], charPos, Length(*state.Lines[state.LinePos-1])-charPos+1))
414+
*state.Lines[state.LinePos-1] = Copy(*state.Lines[state.LinePos-1], 1, charPos-1) + InputKeyPressed + Copy(*state.Lines[state.LinePos-1], charPos, Length(*state.Lines[state.LinePos-1])-charPos+1)
415415
charPos++
416416
}
417417
}

converter.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ func Convert(file File, units []*Unit, w io.Writer) {
2323
[]*ParamGroup{{false, []string{"x"}, &TypeIdent{"byte"}}},
2424
&TypeIdent{"string"},
2525
})
26+
c.defineVar("Copy", &FuncSpec{
27+
[]*ParamGroup{
28+
{false, []string{"s"}, &TypeIdent{"string"}},
29+
{false, []string{"index", "count"}, &TypeIdent{"integer"}},
30+
},
31+
&TypeIdent{"string"},
32+
})
2633
c.defineVar("GetTime", &ProcSpec{[]*ParamGroup{
2734
{true, []string{"h", "m", "s", "s100"}, &TypeIdent{"uint16"}},
2835
}})
@@ -819,34 +826,38 @@ func (c *converter) assignRhs(left Expr, right Expr) {
819826
kind := c.exprKind(right)
820827
spec, _ := c.lookupVarExprType(left)
821828
targetKind := c.specToKind(spec)
822-
converted := c.startConvertExpr(kind, targetKind, right)
829+
end := c.startConvertExpr(kind, targetKind, right)
823830

824831
if parenExpr, isParen := right.(*ParenExpr); isParen {
825832
right = parenExpr.Expr
826833
}
827834
c.expr(right)
828835

829-
if converted {
830-
c.print(")")
836+
if end != "" {
837+
c.print(end)
831838
}
832839
}
833840

834-
func (c *converter) startConvertExpr(kind, targetKind Kind, expr Expr) bool {
841+
func (c *converter) startConvertExpr(kind, targetKind Kind, expr Expr) string {
835842
if targetKind == KindByte && kind == KindString {
836843
constExpr, isConst := expr.(*ConstExpr)
837844
if isConst {
838845
str, isStr := constExpr.Value.(string)
839846
if isStr && len(str) == 1 {
840-
return false
847+
return ""
841848
}
842849
}
843850
}
844851
convertKind := c.convertKind(kind, targetKind)
852+
if convertKind == KindString && kind == KindByte {
853+
c.print("string([]byte{")
854+
return "})"
855+
}
845856
if convertKind != KindUnknown {
846857
c.print(convertKind, "(")
847-
return true
858+
return ")"
848859
}
849-
return false
860+
return ""
850861
}
851862

852863
func (c *converter) procArgs(params []*ParamGroup, args []Expr) {
@@ -883,7 +894,7 @@ func (c *converter) convertKind(kind, targetKind Kind) Kind {
883894

884895
func (c *converter) procArg(targetIsVar bool, targetKind Kind, arg Expr) {
885896
kind := c.exprKind(arg)
886-
converted := c.startConvertExpr(kind, targetKind, arg)
897+
end := c.startConvertExpr(kind, targetKind, arg)
887898
switch arg := arg.(type) {
888899
case *IdentExpr:
889900
isVar := c.isVarParam(arg.Name)
@@ -914,8 +925,8 @@ func (c *converter) procArg(targetIsVar bool, targetKind Kind, arg Expr) {
914925
default:
915926
c.expr(arg)
916927
}
917-
if converted {
918-
c.print(")")
928+
if end != "" {
929+
c.print(end)
919930
}
920931
}
921932

0 commit comments

Comments
 (0)