WeixiangYan/CodeTransOcean huggingface dataset:
Task: Hofstadter-Conway $10,000 sequence





Java: 

public class HofstadterConwaySequence {

    private static int MAX = (int) Math.pow(2, 20) + 1;
    private static int[] HCS = new int[MAX];
    static {
        HCS[1] = 1;
        HCS[2] = 1;
        for ( int n = 3 ; n < MAX ; n++ ) {
            int nm1 = HCS[n - 1];
            HCS[n] = HCS[nm1] + HCS[n - nm1];
        }
    }
    
    public static void main(String[] args) {
        int mNum = 0;
        for ( int m = 1 ; m < 20 ; m++ ) {
            int min = (int) Math.pow(2, m);
            int max = min * 2;
            double maxRatio = 0.0;
            int nVal = 0;
            for ( int n = min ; n <= max ; n ++ ) {
                double ratio = (double) HCS[n] / n;
                if ( ratio > maxRatio ) {
                    maxRatio = Math.max(ratio,  maxRatio);
                    nVal = n;
                }
                if ( ratio >= 0.55 ) {
                    mNum = n;
                }
            }
            System.out.printf("Max ratio between 2^%d and 2^%d is %f at n = %,d%n", m, m+1, maxRatio, nVal);
        }
        System.out.printf("Mallow's number is %d.%n", mNum);
    }

}






Fortran: program conway
  implicit none
  integer :: a(2**20)  
  real    :: b(2**20)  
  real    :: v         
  integer :: nl(1)     
  integer :: i, N, first, second, last, m

  
  a(1:2) = [1, 1]
  b(1:2) = [1.0e0, 0.5e0]
  N = 2
  do i=1,2**20
     last = a(N)
     first = a(last)
     second = a(N-last+1)
     N = N+1
     a(N:N) = first + second
     b(N:N) = a(N:N)/real(N)
  end do

  
  m = 0
  do i=1,19
     v = maxval(b(2**i:2**(i+1)))
     nl = maxloc(b(2**i:2**(i+1)))
     write(*,'(2(a,i0),a,f8.6,a,i0)') &
          'Max. between 2**', i,      &
          ' and 2**', (i+1),          &
          ' is ', v,                  &
          ' at n = ', 2**i+nl(1)-1
     if (m == 0 .and. v < 0.55e0) then
        m = i-1
     end if
  end do

  
  do i=2**(m+1), 2**m,-1
     if (b(i) > 0.55e0) then
        exit
     end if
  end do
  write(*,'(a,i0)') 'Mallows number = ',i

end program conway
Task: Hofstadter-Conway $10,000 sequence







Python: from __future__ import division

def maxandmallows(nmaxpower2):
    nmax = 2**nmaxpower2
    mx = (0.5, 2)
    mxpow2 = []
    mallows = None

    
    
    hc = [None, 1, 1]

    for n in range(2, nmax + 1):
        ratio = hc[n] / n
        if ratio > mx[0]:
            mx = (ratio, n)
        if ratio >= 0.55:
            mallows = n
        if ratio == 0.5:
            print("In the region %7i < n <= %7i: max a(n)/n = %6.4f at  n = %i" %
		  (n//2, n, mx[0], mx[1]))
            mxpow2.append(mx[0])
            mx = (ratio, n)
        hc.append(hc[hc[n]] + hc[-hc[n]])

    return hc, mallows if mxpow2 and mxpow2[-1] < 0.55 and n > 4 else None

if __name__ == '__main__':
    hc, mallows = maxandmallows(20)
    if mallows:
        print("\nYou too might have won $1000 with the mallows number of %i" % mallows)




Fortran: program conway
  implicit none
  integer :: a(2**20)  
  real    :: b(2**20)  
  real    :: v         
  integer :: nl(1)     
  integer :: i, N, first, second, last, m

  
  a(1:2) = [1, 1]
  b(1:2) = [1.0e0, 0.5e0]
  N = 2
  do i=1,2**20
     last = a(N)
     first = a(last)
     second = a(N-last+1)
     N = N+1
     a(N:N) = first + second
     b(N:N) = a(N:N)/real(N)
  end do

  
  m = 0
  do i=1,19
     v = maxval(b(2**i:2**(i+1)))
     nl = maxloc(b(2**i:2**(i+1)))
     write(*,'(2(a,i0),a,f8.6,a,i0)') &
          'Max. between 2**', i,      &
          ' and 2**', (i+1),          &
          ' is ', v,                  &
          ' at n = ', 2**i+nl(1)-1
     if (m == 0 .and. v < 0.55e0) then
        m = i-1
     end if
  end do

  
  do i=2**(m+1), 2**m,-1
     if (b(i) > 0.55e0) then
        exit
     end if
  end do
  write(*,'(a,i0)') 'Mallows number = ',i

end program conway
Task: Hofstadter-Conway $10,000 sequence








VB: Public q() As Long
Sub make_q()
    ReDim q(2 ^ 20)
    q(1) = 1
    q(2) = 1
    Dim l As Long
    For l = 3 To 2 ^ 20
        q(l) = q(q(l - 1)) + q(l - q(l - 1))
    Next l
End Sub
 
Public Sub hcsequence()
    Dim mallows As Long: mallows = -1
    Dim max_n As Long, n As Long
    Dim l As Long, h As Long
    make_q
    For p = 0 To 19
        max_an = 0.5
        l = 2 ^ p: h = l * 2
        For n = l To h
            an = q(n) / n
            If an >= max_an Then
                max_an = an
                max_n = n
            End If
            If an > 0.55 Then
                mallows = n
            End If
        Next n
        Debug.Print "Maximum in range"; Format(l, "@@@@@@@"); " to"; h; String$(7 - Len(CStr(h)), " ");
        Debug.Print "occurs at"; Format(max_n, "@@@@@@@"); ": "; Format(max_an, "0.000000")
    Next p
    Debug.Print "Mallows number is"; mallows
End Sub



Fortran: program conway
  implicit none
  integer :: a(2**20)  
  real    :: b(2**20)  
  real    :: v         
  integer :: nl(1)     
  integer :: i, N, first, second, last, m

  
  a(1:2) = [1, 1]
  b(1:2) = [1.0e0, 0.5e0]
  N = 2
  do i=1,2**20
     last = a(N)
     first = a(last)
     second = a(N-last+1)
     N = N+1
     a(N:N) = first + second
     b(N:N) = a(N:N)/real(N)
  end do

  
  m = 0
  do i=1,19
     v = maxval(b(2**i:2**(i+1)))
     nl = maxloc(b(2**i:2**(i+1)))
     write(*,'(2(a,i0),a,f8.6,a,i0)') &
          'Max. between 2**', i,      &
          ' and 2**', (i+1),          &
          ' is ', v,                  &
          ' at n = ', 2**i+nl(1)-1
     if (m == 0 .and. v < 0.55e0) then
        m = i-1
     end if
  end do

  
  do i=2**(m+1), 2**m,-1
     if (b(i) > 0.55e0) then
        exit
     end if
  end do
  write(*,'(a,i0)') 'Mallows number = ',i

end program conway
Task: Bitmap



C#: public class Bitmap
{
    public struct Color
    {
        public byte Red { get; set; }
        public byte Blue { get; set; }
        public byte Green { get; set; }
    }
    Color[,] _imagemap;
    public int Width { get { return _imagemap.GetLength(0); } }
    public int Height { get { return _imagemap.GetLength(1); } }
    public Bitmap(int width, int height)
    {
        _imagemap = new Color[width, height];
    }
    public void Fill(Color color)
    {
        for (int y = 0; y < Height; y++)
            for (int x = 0; x < Width; x++)
            {
                _imagemap[x, y] = color;
            }
    }
    public Color GetPixel(int x, int y)
    {
        return _imagemap[x, y];
    }
    public void SetPixel(int x, int y, Color color)
    {
        _imagemap[x, y] = color;
    }
}









Ada: package Bitmap_Store is
   type Luminance is mod 2**8;
   type Pixel is record
      R, G, B : Luminance := Luminance'First;
   end record;
   Black : constant Pixel := (others => Luminance'First);
   White : constant Pixel := (others => Luminance'Last);
   type Image is array (Positive range <>, Positive range <>) of Pixel;
 
   procedure Fill (Picture : in out Image; Color : Pixel);
 
   procedure Print (Picture : Image);
 
   type Point is record
      X, Y : Positive;
   end record;
end Bitmap_Store;
Task: Bitmap

C: #ifndef _IMGLIB_0
#define _IMGLIB_0

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <math.h>
#include <sys/queue.h>

typedef unsigned char color_component;
typedef color_component pixel[3];
typedef struct {
    unsigned int width;
    unsigned int height;
    pixel * buf;
} image_t;
typedef image_t * image;

image alloc_img(unsigned int width, unsigned int height);
void free_img(image);
void fill_img(image img,
        color_component r,
        color_component g,
        color_component b );
void put_pixel_unsafe(
       	image img,
        unsigned int x,
        unsigned int y,
        color_component r,
        color_component g,
        color_component b );
void put_pixel_clip(
       	image img,
        unsigned int x,
        unsigned int y,
        color_component r,
        color_component g,
        color_component b );
#define GET_PIXEL(IMG, X, Y) (IMG->buf[ ((Y) * IMG->width + (X)) ])
#endif











Ada: package Bitmap_Store is
   type Luminance is mod 2**8;
   type Pixel is record
      R, G, B : Luminance := Luminance'First;
   end record;
   Black : constant Pixel := (others => Luminance'First);
   White : constant Pixel := (others => Luminance'Last);
   type Image is array (Positive range <>, Positive range <>) of Pixel;
 
   procedure Fill (Picture : in out Image; Color : Pixel);
 
   procedure Print (Picture : Image);
 
   type Point is record
      X, Y : Positive;
   end record;
end Bitmap_Store;
Task: Bitmap


C++: #include <iostream>
#include <boost/gil/gil_all.hpp>
int main()
{
    using namespace boost::gil;
    
    rgb8_image_t img(30, 40);

    
    rgb8_pixel_t red(255, 0, 0);
    fill_pixels(view(img), red);

    
    rgb8_pixel_t blue(0, 0, 255);
    view(img)(10, 20) = blue;

    
    rgb8_pixel_t px = const_view(img)(11, 20);
    std::cout << "the pixel at 11, 20 is " << (unsigned)px[0] << ':' << (unsigned)px[1] << ':' << (unsigned)px[2]  << '\n';
}










Ada: package Bitmap_Store is
   type Luminance is mod 2**8;
   type Pixel is record
      R, G, B : Luminance := Luminance'First;
   end record;
   Black : constant Pixel := (others => Luminance'First);
   White : constant Pixel := (others => Luminance'Last);
   type Image is array (Positive range <>, Positive range <>) of Pixel;
 
   procedure Fill (Picture : in out Image; Color : Pixel);
 
   procedure Print (Picture : Image);
 
   type Point is record
      X, Y : Positive;
   end record;
end Bitmap_Store;
Task: Bitmap




Go: package main

import (
	"bytes"
	"fmt"
	"image"
	"image/color"
	"image/draw"
	"image/png"
)

func main() {
	
	r := image.Rect(0, 0, 300, 240)

	
	im := image.NewNRGBA(r)

	
	var (
		red  = color.RGBA{0xff, 0x00, 0x00, 0xff}
		blue = color.RGBA{0x00, 0x00, 0xff, 0xff}
	)

	
	draw.Draw(im, r, &image.Uniform{red}, image.ZP, draw.Src)

	
	im.Set(10, 20, blue)
	im.Set(20, 30, color.Black)
	im.Set(30, 40, color.RGBA{0x10, 0x20, 0x30, 0xff})

	
	
	
	
	c1 := im.At(0, 0)
	c2 := im.At(10, 20)

	
	redc, greenc, bluec, _ := c1.RGBA()
	redc, greenc, bluec, _ = im.At(30, 40).RGBA()

	
	var buf bytes.Buffer
	err := png.Encode(&buf, im)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("Image size:", im.Bounds().Dx(), "×", im.Bounds().Dy())
	fmt.Println(buf.Len(), "bytes when encoded as PNG.")
	fmt.Printf("Pixel at %7v is %v\n", image.Pt(0, 0), c1)
	fmt.Printf("Pixel at %7v is %#v\n", image.Pt(10, 20), c2) 
	fmt.Printf("Pixel at %7v has R=%d, G=%d, B=%d\n",
		image.Pt(30, 40), redc, greenc, bluec)
}








Ada: package Bitmap_Store is
   type Luminance is mod 2**8;
   type Pixel is record
      R, G, B : Luminance := Luminance'First;
   end record;
   Black : constant Pixel := (others => Luminance'First);
   White : constant Pixel := (others => Luminance'Last);
   type Image is array (Positive range <>, Positive range <>) of Pixel;
 
   procedure Fill (Picture : in out Image; Color : Pixel);
 
   procedure Print (Picture : Image);
 
   type Point is record
      X, Y : Positive;
   end record;
end Bitmap_Store;
Task: Bitmap





Java: import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

public class BasicBitmapStorage {

    private final BufferedImage image;

    public BasicBitmapStorage(int width, int height) {
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }

    public void fill(Color c) {
        Graphics g = image.getGraphics();
        g.setColor(c);
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
    }

    public void setPixel(int x, int y, Color c) {
        image.setRGB(x, y, c.getRGB());
    }

    public Color getPixel(int x, int y) {
        return new Color(image.getRGB(x, y));
    }

    public Image getImage() {
        return image;
    }
}







Ada: package Bitmap_Store is
   type Luminance is mod 2**8;
   type Pixel is record
      R, G, B : Luminance := Luminance'First;
   end record;
   Black : constant Pixel := (others => Luminance'First);
   White : constant Pixel := (others => Luminance'Last);
   type Image is array (Positive range <>, Positive range <>) of Pixel;
 
   procedure Fill (Picture : in out Image; Color : Pixel);
 
   procedure Print (Picture : Image);
 
   type Point is record
      X, Y : Positive;
   end record;
end Bitmap_Store;
Task: Bitmap








VB: 
<System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)> _
Public Structure Rgb

    <FieldOffset(0)> _
    Public Rgb As Integer

    <FieldOffset(0)> _
    Public B As Byte

    <FieldOffset(1)> _
    Public G As Byte

    <FieldOffset(2)> _
    Public R As Byte

    Public Sub New(ByVal r As Byte, ByVal g As Byte, ByVal b As Byte)
        Me.R = r
        Me.G = g
        Me.B = b
    End Sub

End Structure




Ada: package Bitmap_Store is
   type Luminance is mod 2**8;
   type Pixel is record
      R, G, B : Luminance := Luminance'First;
   end record;
   Black : constant Pixel := (others => Luminance'First);
   White : constant Pixel := (others => Luminance'Last);
   type Image is array (Positive range <>, Positive range <>) of Pixel;
 
   procedure Fill (Picture : in out Image; Color : Pixel);
 
   procedure Print (Picture : Image);
 
   type Point is record
      X, Y : Positive;
   end record;
end Bitmap_Store;
Task: Bitmap

C: #ifndef _IMGLIB_0
#define _IMGLIB_0

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <math.h>
#include <sys/queue.h>

typedef unsigned char color_component;
typedef color_component pixel[3];
typedef struct {
    unsigned int width;
    unsigned int height;
    pixel * buf;
} image_t;
typedef image_t * image;

image alloc_img(unsigned int width, unsigned int height);
void free_img(image);
void fill_img(image img,
        color_component r,
        color_component g,
        color_component b );
void put_pixel_unsafe(
       	image img,
        unsigned int x,
        unsigned int y,
        color_component r,
        color_component g,
        color_component b );
void put_pixel_clip(
       	image img,
        unsigned int x,
        unsigned int y,
        color_component r,
        color_component g,
        color_component b );
#define GET_PIXEL(IMG, X, Y) (IMG->buf[ ((Y) * IMG->width + (X)) ])
#endif
Task: Bitmap



C#: public class Bitmap
{
    public struct Color
    {
        public byte Red { get; set; }
        public byte Blue { get; set; }
        public byte Green { get; set; }
    }
    Color[,] _imagemap;
    public int Width { get { return _imagemap.GetLength(0); } }
    public int Height { get { return _imagemap.GetLength(1); } }
    public Bitmap(int width, int height)
    {
        _imagemap = new Color[width, height];
    }
    public void Fill(Color color)
    {
        for (int y = 0; y < Height; y++)
            for (int x = 0; x < Width; x++)
            {
                _imagemap[x, y] = color;
            }
    }
    public Color GetPixel(int x, int y)
    {
        return _imagemap[x, y];
    }
    public void SetPixel(int x, int y, Color color)
    {
        _imagemap[x, y] = color;
    }
}
Task: Bitmap


C++: #include <iostream>
#include <boost/gil/gil_all.hpp>
int main()
{
    using namespace boost::gil;
    
    rgb8_image_t img(30, 40);

    
    rgb8_pixel_t red(255, 0, 0);
    fill_pixels(view(img), red);

    
    rgb8_pixel_t blue(0, 0, 255);
    view(img)(10, 20) = blue;

    
    rgb8_pixel_t px = const_view(img)(11, 20);
    std::cout << "the pixel at 11, 20 is " << (unsigned)px[0] << ':' << (unsigned)px[1] << ':' << (unsigned)px[2]  << '\n';
}
Task: Bitmap





Java: import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

public class BasicBitmapStorage {

    private final BufferedImage image;

    public BasicBitmapStorage(int width, int height) {
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }

    public void fill(Color c) {
        Graphics g = image.getGraphics();
        g.setColor(c);
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
    }

    public void setPixel(int x, int y, Color c) {
        image.setRGB(x, y, c.getRGB());
    }

    public Color getPixel(int x, int y) {
        return new Color(image.getRGB(x, y));
    }

    public Image getImage() {
        return image;
    }
}
Task: Bitmap








VB: 
<System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)> _
Public Structure Rgb

    <FieldOffset(0)> _
    Public Rgb As Integer

    <FieldOffset(0)> _
    Public B As Byte

    <FieldOffset(1)> _
    Public G As Byte

    <FieldOffset(2)> _
    Public R As Byte

    Public Sub New(ByVal r As Byte, ByVal g As Byte, ByVal b As Byte)
        Me.R = r
        Me.G = g
        Me.B = b
    End Sub

End Structure
Task: Bitmap




Go: package main

import (
	"bytes"
	"fmt"
	"image"
	"image/color"
	"image/draw"
	"image/png"
)

func main() {
	
	r := image.Rect(0, 0, 300, 240)

	
	im := image.NewNRGBA(r)

	
	var (
		red  = color.RGBA{0xff, 0x00, 0x00, 0xff}
		blue = color.RGBA{0x00, 0x00, 0xff, 0xff}
	)

	
	draw.Draw(im, r, &image.Uniform{red}, image.ZP, draw.Src)

	
	im.Set(10, 20, blue)
	im.Set(20, 30, color.Black)
	im.Set(30, 40, color.RGBA{0x10, 0x20, 0x30, 0xff})

	
	
	
	
	c1 := im.At(0, 0)
	c2 := im.At(10, 20)

	
	redc, greenc, bluec, _ := c1.RGBA()
	redc, greenc, bluec, _ = im.At(30, 40).RGBA()

	
	var buf bytes.Buffer
	err := png.Encode(&buf, im)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("Image size:", im.Bounds().Dx(), "×", im.Bounds().Dy())
	fmt.Println(buf.Len(), "bytes when encoded as PNG.")
	fmt.Printf("Pixel at %7v is %v\n", image.Pt(0, 0), c1)
	fmt.Printf("Pixel at %7v is %#v\n", image.Pt(10, 20), c2) 
	fmt.Printf("Pixel at %7v has R=%d, G=%d, B=%d\n",
		image.Pt(30, 40), redc, greenc, bluec)
}
Task: Bitmap






PHP: class Bitmap {
  public $data;
  public $w;
  public $h;
  public function __construct($w = 16, $h = 16){
    $white = array_fill(0, $w, array(255,255,255));
    $this->data = array_fill(0, $h, $white);
    $this->w = $w;
    $this->h = $h;
  }

  public function fill($x = 0, $y = 0, $w = null, $h = null, $color = array(0,0,0)){
    if (is_null($w)) $w = $this->w;
    if (is_null($h)) $h = $this->h;
    $w += $x;
    $h += $y;
    for ($i = $y; $i < $h; $i++){
      for ($j = $x; $j < $w; $j++){
        $this->setPixel($j, $i, $color);
      }
    }
  }
  public function setPixel($x, $y, $color = array(0,0,0)){
    if ($x >= $this->w) return false;
    if ($x < 0) return false;
    if ($y >= $this->h) return false;
    if ($y < 0) return false;
    $this->data[$y][$x] = $color;
  }
  public function getPixel($x, $y){
    return $this->data[$y][$x];
  }
}

$b = new Bitmap(16,16);
$b->fill();
$b->fill(2, 2, 18, 18, array(240,240,240));
$b->setPixel(0, 15, array(255,0,0));
print_r($b->getPixel(3,3)); //(240,240,240)
Task: Bitmap






PHP: class Bitmap {
  public $data;
  public $w;
  public $h;
  public function __construct($w = 16, $h = 16){
    $white = array_fill(0, $w, array(255,255,255));
    $this->data = array_fill(0, $h, $white);
    $this->w = $w;
    $this->h = $h;
  }

  public function fill($x = 0, $y = 0, $w = null, $h = null, $color = array(0,0,0)){
    if (is_null($w)) $w = $this->w;
    if (is_null($h)) $h = $this->h;
    $w += $x;
    $h += $y;
    for ($i = $y; $i < $h; $i++){
      for ($j = $x; $j < $w; $j++){
        $this->setPixel($j, $i, $color);
      }
    }
  }
  public function setPixel($x, $y, $color = array(0,0,0)){
    if ($x >= $this->w) return false;
    if ($x < 0) return false;
    if ($y >= $this->h) return false;
    if ($y < 0) return false;
    $this->data[$y][$x] = $color;
  }
  public function getPixel($x, $y){
    return $this->data[$y][$x];
  }
}

$b = new Bitmap(16,16);
$b->fill();
$b->fill(2, 2, 18, 18, array(240,240,240));
$b->setPixel(0, 15, array(255,0,0));
print_r($b->getPixel(3,3)); //(240,240,240)






Ada: package Bitmap_Store is
   type Luminance is mod 2**8;
   type Pixel is record
      R, G, B : Luminance := Luminance'First;
   end record;
   Black : constant Pixel := (others => Luminance'First);
   White : constant Pixel := (others => Luminance'Last);
   type Image is array (Positive range <>, Positive range <>) of Pixel;
 
   procedure Fill (Picture : in out Image; Color : Pixel);
 
   procedure Print (Picture : Image);
 
   type Point is record
      X, Y : Positive;
   end record;
end Bitmap_Store;
Task: Tokenize a string with escaping



C#: using System;
using System.Text;
using System.Collections.Generic;

public class TokenizeAStringWithEscaping
{
    public static void Main() {
        string testcase = "one^|uno||three^^^^|four^^^|^cuatro|";
        foreach (var token in testcase.Tokenize(separator: '|', escape: '^')) {
            Console.WriteLine(": " + token); 
        }
    }
}

public static class Extensions
{
    public static IEnumerable<string> Tokenize(this string input, char separator, char escape) {
        if (input == null) yield break;
        var buffer = new StringBuilder();
        bool escaping = false;
        foreach (char c in input) {
            if (escaping) {
                buffer.Append(c);
                escaping = false;
            } else if (c == escape) {
                escaping = true;
            } else if (c == separator) {
                yield return buffer.Flush();
            } else {
                buffer.Append(c);
            }
        }
        if (buffer.Length > 0 || input[input.Length-1] == separator) yield return buffer.Flush();
    }
    
    public static string Flush(this StringBuilder stringBuilder) {
        string result = stringBuilder.ToString();
        stringBuilder.Clear();
        return result;
    }
}









Ada: with Ada.Text_Io;
with Ada.Containers.Indefinite_Vectors;
with Ada.Strings.Unbounded;

procedure Tokenize is

   package String_Vectors is
     new Ada.Containers.Indefinite_Vectors (Positive, String);
   use String_Vectors;

   function Split (Text      : String;
                   Separator : Character := '|';
                   Escape    : Character := '^') return Vector
   is
      use Ada.Strings.Unbounded;
      Result  : Vector;
      Escaped : Boolean := False;
      Accu    : Unbounded_String;
   begin

      for Char of Text loop

         case Escaped is

            when False =>
               if Char = Escape then
                  Escaped := True;
               elsif Char = Separator then
                  Append (Result, To_String (Accu));
                  Accu := Null_Unbounded_String;
               else
                  Append (Accu, Char);
               end if;

            when True =>
               Append (Accu, Char);
               Escaped := False;

         end case;

      end loop;
      Append (Result, To_String (Accu));

      return Result;
   end Split;

   procedure Put_Vector (List : Vector) is
      use Ada.Text_Io;
   begin
      for Element of List loop
         Put ("'"); Put (Element); Put ("'"); New_Line;
      end loop;
   end Put_Vector;

begin
   Put_Vector (Split ("one^|uno||three^^^^|four^^^|^cuatro|"));
end Tokenize;