StringReserve

Reserva na memória para uma string um buffer de tamanho especificado.

bool  StringReserve(
   string&    string_var,       // strings
   uint       new_capacity      // tamanho do buffer para armazenar a string.
   );

Parâmetros

string_var

[in][out]  String para a qual você deseja alterar o tamanho do buffer.

new_capacity

[in]  Tamanho do buffer necessário de acordo com a string. Se o novo tamanho new_capacity for menor que o comprimento da string, o tamanho do buffer atual não será alterado.

Valor retornado

Se for bem-sucedido, retorna true, caso contrário, false. Para obter o código do erro é preciso chamar a função GetLastError().

Observação

O tamanho da string não é igual ao tamanho do buffer para armazenar a string. Como regra, ao criar uma string, o buffer para ela é alocado com reserva. A função StringReserve() permite controlar o tamanho do buffer e especificar o tamanho ideal para operações futuras.

Ao contrário da StringInit(), a função StringReserve() ) não altera o conteúdo da string e não a preenche com caracteres.

Exemplo:

void OnStart()
  {
   string s;
//--- verifica a velocidade sem usar StringReserve
   ulong t0=GetMicrosecondCount();
   for(int i=0; i< 1024; i++)
      s+=" "+(string)i;
   ulong msc_no_reserve=GetMicrosecondCount()-t0;
   s=NULL;
//--- agora mede usando a StringReserve
   StringReserve(s,1024 * 3);
   t0=GetMicrosecondCount();
   for(int i=0; i< 1024; i++)
      s+=" "+(string)i;
   ulong msc_reserve=GetMicrosecondCount()-t0;
//--- verifica o tempo
   Print("Test with StringReserve passed for "+(string)msc_reserve+" msc");   
   Print("Test without StringReserve passed for "+(string)msc_no_reserve+" msc");         
/* Resultado:
     Test with StringReserve passed for 50 msc
     Test without StringReserve passed for 121 msc
*/
  }

Veja também

StringBufferLen, StringSetLength, StringInit, StringSetCharacter