现在的位置: 首页 > 综合 > 正文

ADA程序实例(字符串类型及其简单处理)

2013年12月09日 ⁄ 综合 ⁄ 共 3557字 ⁄ 字号 评论关闭

ADA语言内建的字符串类型是定长类型,基本接近相当于C的静态字符数组。对ADA而言,String也完全是通过字符数组的严格定义派生出来的(可参见wikibooks关于ADA类型系统的条目;关于ADA的复杂的类型系统需要另行撰文)。定长字符类型对应的操作包在Ada.Strings.Fixed中。另外String类型也有很多这个类型的Attribute。这类字符串通常在编译期决定长度(由其界限参数指定或所附值决定);但对ADA而言,这种长度确定也可以宽松一点:字符串和所有数据变量实例一样都定义在变量定义区,它可以不限定长度,而由一个函数返回的字符串确定;另一方面,对于一个子程序变量定义区的字符串,它可以用一个数值变量为其在运行时配置长度,这有点像扩展后C语言中的栈空间分配一样(其实背后运行机理也是这样,而这个特性也适用于ADA的其他如数组Array上)。而在字符串作为参数传给子程序的时候,这个形参的类型必须定义成string,而不含长度节点,这个也很容易理解。至此,对于一般的字符串处理,充分利用这种栈空间分配的特性,合理创建字符变量,这种定长字符串类型已经足够了。

另外ADA的类库也提供了字符串类型,相当于是一种字符串对象了,主要有两种,一种是Bounded(有界)类型,另一种是Unbounded(无界)类型。Bounded类型的使用需要实例化定义它的一个泛型包,而这个泛型包的参数只是一个数值,用于指定这个包提供的Bounded字符串的最大长度,这样估计包里的功能根据这个长度进行一些存储准备或策略配置等,而这个包产生每个字符串实例的长度也就不能超过这个界限。而无界类型是不需要做这个指定而字符串也能无限增长。

ADA字符串操作符除了已经见到的乘法用于复制串以外,对字符串连接则用&,而不是一般语言中用的+(当然用户可以去改,但这不符合ADA的通常实践)。需要注意的是,如果连接两个定长字符类型变量,如果赋值给一个定长类型变量,这个变量必须正正好好和这两个连接起来的字符串的长度相等,这个比较麻烦,但这是没办法的;除了连接外,ADA还提供了一些字符函数的插入替换操作等(Replace_Slice, Overwrite, Insert等),也是比较方便的。ADA的定长类型求子字符串只需要在数组下标中指定即可;但如果在字符串对象上进行的话,由于ADA不支持数组下标的运算符重载,所以必须使用函数如Slice/Replace_Slice来提取设置子串或Element/Replace_Element来提取设置单个字符。而string,Bounded_String和Unbounded_String之间转换目前似乎只提供了string和后两者之间的互转,对于这些ADA库可能还有待更新扩展。

with Ada.Text_IO;
use Ada.Text_IO;

with Ada.Strings.Fixed;
use Ada.Strings.Fixed;

with Ada.Strings.Bounded;
with Ada.Strings.Unbounded;

with Ada.Command_Line;

procedure adastrings is
  
  package CL renames Ada.Command_Line; -- shorten the package name
  package Bounded is new Ada.Strings.Bounded.Generic_Bounded_Length(Max => 128);
  use Bounded;
  use Ada.Strings.Unbounded;

  fixed_str_without_sizespec : string := "size determined by designated value";
  fixed_str_from_cmd_line : string := CL.Argument(1);
  fixed_str : string(1..32) := 32 * "0"; -- has to be 32
  fixed_str2 : string(1..32) := 32 * "1";
  fixed_str3 : string(1..64);
  bounded_str : Bounded_String;
  unbounded_str : UnBounded_String;
    
  -- create a string 'dynamically' by duplicating a string given times
  function duplicate_string(str: string; occurs : integer) return string is
    result : string := occurs * str;
  begin
    return result;
  end duplicate_string;
  
  -- create a string 'dynmically' by returning a string of any length 
  -- specified at run time
  function produce_blank_string(length : integer) return string is
    result : string(1..length) := length * ' ';
  begin
    return result;
  end produce_blank_string;

  procedure print_string (str : string) is
  begin
    put_line(str);
  end print_string;

  procedure print_string (bounded_str : bounded_string) is
  begin
    put_line(To_String(bounded_str));
  end print_string;

  procedure print_string (unbounded_str : unbounded_string) is
  begin
    put_line(To_String(unbounded_str));
  end print_string;

begin
  print_string("demo program for ada string types");
  print_string(fixed_str);

  fixed_str3 := fixed_str2 & fixed_str;

  print_string(fixed_str3);

  print_string(fixed_str2(1..16));

  fixed_str := Overwrite(fixed_str, 17, fixed_str2(1..16));

  print_string(fixed_str);
  print_string(fixed_str_without_sizespec);
  print_string(fixed_str_from_cmd_line);

  bounded_str := bounded_str & fixed_str;

  print_string(bounded_str);

  unbounded_str := To_Unbounded_String(To_String(bounded_str));
  unbounded_str := unbounded_str & "123";

  print_string(unbounded_str);

  Replace_Slice(unbounded_str, 3, 10, "replaced");
  print_string(unbounded_str);
 
  print_string(duplicate_string("Sample", 5));
  print_string(produce_blank_string(12));
  
end adastrings;

执行结果:

> adastrings.exe argument1
demo program for ada string types
00000000000000000000000000000000
1111111111111111111111111111111100000000000000000000000000000000
1111111111111111
00000000000000001111111111111111
size determined by designated value
argument1
00000000000000001111111111111111
00000000000000001111111111111111123
00replaced0000001111111111111111123
SampleSampleSampleSampleSample

抱歉!评论已关闭.