Spec-Zone.ru › Octave 8

A.1.3 Строки символов в файлах Oct

Строка символов в Octave — это просто специальный Array класс. Рассмотрим пример:

#include <octave/oct.h>

DEFUN_DLD (stringdemo, args, , "String Demo")
{
  if (args.length () != 1)
    print_usage ();

  octave_value_list retval;

  charMatrix ch = args(0).char_matrix_value ();

  retval(1) = octave_value (ch, '\'');  // Single Quote String

  octave_idx_type nr = ch.rows ();

  for (octave_idx_type i = 0; i < nr / 2; i++)
    {
      std::string tmp = ch.row_as_string (i);

      ch.insert (ch.row_as_string (nr-i-1).c_str (), i, 0);
      ch.insert (tmp.c_str (), nr-i-1, 0);
    }

  retval(0) = octave_value (ch, '"');  // Double Quote String

  return retval;
}

Пример использования этой функции:

s0 = ["First String"; "Second String"];
[s1,s2] = stringdemo (s0)
⇒ s1 = Second String
        First String

⇒ s2 = First String
        Second String

typeinfo (s2)
⇒ sq_string
typeinfo (s1)
⇒ string

Ещё одна сложность при работе со строками в Octave — это различие между строками в одинарных и двойных кавычках. Чтобы определить, содержит ли octave_value строку в одинарных или двойных кавычках, используйте один из предикатных тестов, показанных ниже.

if (args(0).is_sq_string ())
  octave_stdout << "First argument is a single quoted string\n";
else if (args(0).is_dq_string ())
  octave_stdout << "First argument is a double quoted string\n";

Обратите внимание, что оба типа строк представлены типом charNDArray, и поэтому при присвоении octave_value, тип строки должен быть указан. Например:

octave_value_list retval;
charNDArray ch;
…
// Create single quoted string
retval(1) = octave_value (ch);   // default constructor is sq_string
           OR
retval(1) = octave_value (ch, '\'');  // explicitly create sq_string

// Create a double quoted string
retval(0) = octave_value (ch, '"');

© 1996–2022 The Octave Project Developers
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://docs.octave.org/v7.2.0/Character-Strings-in-Oct_002dFiles.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API