c++c++builderansistring

Truncate an AnsiString


I'm working with Rad Studio, c++ builder. The size of the AnsiString size is ~2^31 characters. How should I check the length?

if(ExportFileName.Length() > ??? )
  {
    ExportFileName. ???
  }
  m_ActionsHelper.LastPdfFile = ExportFileName;

Solution

  • As I see it in the reference, both parameters of Delete are int, which maximum value way lower than 2^31. It seems you don't need such a check.

    Anyway, if you'd like to get integer power of 2, you can use binary shift operator:

    1ull << 31
    

    Binary shift operator manipulates bits of integer in such a way that all bits are shifted in the required direction. For example,

    Operation   Bits   Shifted bits 10-based
    1 << 1    00000001   00000010      2
    1 << 2    00000001   00000100      4
    4 << 2    00000100   00010000      16
    

    And so on. So 1ull << 31 is 2^31. ull means we use 64-bits number, because int is to small for it.

    To delete the surplus tail using Delete it should be like

    ExportFileName.Delete(1 << 10, ExportFileName.Length());
    

    or

    ExportFileName.SetLength(1 << 10);
    

    And probably you don't need to check the length beforehand. Just Delete or SetLength. If it already satisfies, no action will be performed.