简体   繁体   English

Ada:如何表示java字符串?

[英]Ada: How to represent a java string?

I need some tips/help with one of my home project in Ada. 我需要一些关于我在Ada的家庭项目的提示/帮助。 So i need to do a J_String_Package, but i don't really know how to represent my J_string type. 所以我需要做一个J_String_Package,但我真的不知道如何表示我的J_string类型。 The specification asks me to: "represent J_String type as an opaque discriminant record. For the inner representation of the string, use the Standard String type. The discriminant determines the size of the string that is contained in the J_String type." 规范要求我:“将J_String类型表示为不透明的判别式记录。对于字符串的内部表示,使用标准字符串类型。判别式确定J_String类型中包含的字符串的大小。” My .ads so far: 我的.ads到目前为止:

package J_String_Pkg is
    type J_String(Size: Positive) is limited private;

   --methods etc    

private
    type J_String(Size: Positive) is record
        --i need some help here!! :)
    end record;
end J_String_Pkg;

Thank you for every help! 谢谢你的帮助!

You need something like this: 你需要这样的东西:

type J_String(Size: Positive) is record
   Contents : String (1 .. Size);
end record;

which corresponds closely to one of the examples in the Ada Reference Manual ( ARM 3.7(33) ). 这与Ada参考手册( ARM 3.7(33) )中的一个示例非常吻合。

One thing to watch out for: your code, without a default for the discriminant, means that once created you won't be able to change the Size of a J_String . 需要注意的一点是:您的代码没有判别式的默认值,这意味着一旦创建,您将无法更改J_StringSize The example from the ARM, 来自ARM的例子,

type Buffer(Size : Buffer_Size := 100)  is
   record
      Pos   : Buffer_Size := 0;
      Value : String(1 .. Size);
   end record;

does allow you to change the size of an instance, at the cost of preallocating Buffer_Size characters (at any rate, with GNAT). 确实允许您更改实例的大小,在预分配的成本Buffer_Size字符(无论如何,与GNAT)。 You do not want to do this with Positive ; 不想这样做Positive ; most computers don't have 2 gigabytes of RAM to spare! 大多数计算机没有2千兆字节的RAM备用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM