AdaCore / learn

Sources for learn.adacore.com
https://learn.adacore.com
Creative Commons Attribution 4.0 International
92 stars 37 forks source link

A few clarifications on Unbounded String #1065

Closed Heziode closed 1 week ago

Heziode commented 2 months ago

In the String page, we can see this:

https://github.com/AdaCore/learn/blob/2183de64d50bf6c59e4413e8dd475fd4ee4fd552/content/courses/intro-to-ada/chapters/standard_library_strings.rst#L19-L20

The fact is, in computer, there is no infinite size. The majority of people will never reach the limit, but the fact is:

The language-defined package Strings.Unbounded provides a private type Unbounded_String and a set of operations. An object of type Unbounded_String represents a String whose low bound is 1 and whose length can vary conceptually between 0 and Natural'Last. The subprograms for fixed-length string handling are either overloaded directly for Unbounded_String, or are modified as necessary to reflect the flexibility in length. Since the Unbounded_String type is private, relevant constructor and selector operations are provided.

Source: ARM A.4.5 Unbounded-Length String Handling

And from 3.5.4 Integer Types#21:

In an implementation, the range of Integer shall include the range –2**15+1 .. +2**15–1.

So, the Unbounded String max length is 32_767 elements. If someone wants to process a large (unbounded) string, it could be interesting to redirect it to another type, like GNATCOLL.Strings for instance.

raph-amiard commented 2 months ago

So, the Unbounded String max length is 32_767 elements.

It is at least 32_767 elements. In practice on almost all platforms, native or otherwise, Integer'Last will be 2**31-1.

If someone wants to process a large (unbounded) string, it could be interesting to redirect it to another type, like GNATCOLL.Strings for instance.

GNATCOLL.Strings has the same indexing subtype (Natural), so if you ever were to use a platform where Integer is too small, you would have the problem with both unbounded string types. That's however very unlikely.

@gusthoff if you agree, let's keep this to reformulate along the lines of "their maximum length is Integer'Last". It might be worthwhile to precise that the big difference between bounded and unbounded is the use of heap allocation.

gusthoff commented 1 month ago

@gusthoff if you agree, let's keep this to reformulate along the lines of "their maximum length is Integer'Last". It might be worthwhile to precise that the big difference between bounded and unbounded is the use of heap allocation.

Yes, I've just added an admonition that explains these details. Thanks for suggestion!