Tokenize a string with escaping: Difference between revisions

Tokenize a string with escaping en FreeBASIC
(Added solution for Action!)
(Tokenize a string with escaping en FreeBASIC)
Line 1,303:
 
In this example the DO-loop relentlessly steps through the text, and in general this would not be convenient. Normally, token identification proceeds within a much larger context where one would not discard the token immediately after it is isolated, and rather than copying the text hither and thither, one might prefer to identify it in-place, say with variables <code>L1</code> and <code>L2</code> identifying the start and end positions within the working area. In such a case there would no longer be a need for a variable <code>TOKEN</code> and the angst of deciding on a suitable maximum size. This would also make it easier in any error messages to show context and provenance. However, the bizarre miscegnation of "escape" sequences (especially confusing within text ''literals''), means that the source text does not necessarily constitute the text of the token.
 
 
=={{header|FreeBASIC}}==
{{trans|Ring}}
<lang freebasic>Sub tokenize(cadena As String, separador As String, escape As String)
Dim As Integer campo = 1
Dim As Boolean escapando = false
Dim As String char
Print ""; campo; " ";
For i As Integer = 1 To Len(cadena)
char = Mid(cadena, i, 1)
If escapando Then
Print char;
escapando = false
Else
Select Case char
Case separador
Print
campo += 1
Print ""; campo; " ";
Case escape
escapando = true
Case Else
Print char;
End Select
End If
Next i
Print
End Sub
 
tokenize("one^|uno||three^^^^|four^^^|^cuatro|", "|", "^")
Sleep</lang>
{{out}}
<pre>
Igual que la entrada de Ring.
</pre>
 
 
=={{header|Go}}==
2,130

edits