Martin Schwartz > Convert-Context > Convert::Context

Download:
Convert-Context-0.501.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Source  

NAME ^

Convert::Context - an Attributed Text data type

- ALPHA - release

$Revision: 1.77 $ $Date: 1998/10/03 22:21:23 $

SYNOPSIS ^

See below.

DESCRIPTION ^

Convert::Context maintains attributed strings. It allows you to access those strings similar to perl's normal strings.

An attributed string is a string to that attributes are connected at certain string positions. An attribute can be everything scalar: numbers, strings, references are welcome. Attributes are not part of the string. Semantics of the attributes have to be done by the applying code.

What does this mean?

A basic work for a text system is to localize a certain text part. This is trivial if you have only plain text to look at. It is no longer trivial, if you have attributes or entries among your text like: bold, italic, bookmarks and so on. One has two strategies to mingle attributes with a string:

  1. You can enrich the text by inserting control codes. E.g., if you have a line with two bold words:

    (A) "The word bold is always bold"

    it would look (here with HTML controls) like:

    (B) "The word <b>bold</b> is always <b>bold</b>"

    If you would look for the text "bold is" in (B) with perls m// operator, you'd fail. You would have to strip the HTML control sequences first. This is an ok method, but not used here.

  2. You can maintain separate lists, holding at which position of the text which control codes are stored. This is, what Convert::Context does. The example from above would look like:
       offset    0---------1---------2-------
       text      The word bold is always bold
       attrib   (0        1   0          1   )

    Internally this is stored as:

      $Context = {
         "T" => \("The word bold is always bold"),
         "A" => [0, 1,  0,  1]
         "O" => [0, 9, 13, 24],
      }

    The maintainance of these lists is a little bit tricky, so what a luck, that you don't need to care about this.

Do not rely on this internal representation, as it might change. E.g. it could happen, that "O" in future stores relative offsets instead of absolute.

Available Methods

acmp

sub { $Code } = $Ct -> acmp (sub { $Code })

When two attribs shall be compared, this normally is done stringwise, using function "cmp". If this is not practical for you, with acmp you could provide a new compare function, similar to the way you do when using sort.

The standard behavior is implemented this way:

   $Ct -> acmp (sub { $_[0] cmp $_[1] })

Note: The code provided via acmp is not used when comparing identity. That means: $Ct1 -> eq ($Ct1) is always true.

append

$Ct1 = $Ct1 -> append ($Ct2||$str2||$strR2, ...)

Appends all strings, string references or Contexts to the end of Context1.

attrib

Attrib is used to yield and change the attributes of a Context. It can be called several ways.

(1) $attrib = $Ct -> attrib ($pos [,[$attrib]])

When called in a scalar context with only $pos as parameter, attrib returns the attribute of Context at character position $pos. You can can set the attribute by specifying the new one as a list reference (not recommended).

(2) ([@attrib], [@offset]) = $Ct -> attrib ($pos, $len)

When called in an array context, a list with references to a free usable attrib array and a free usable offset array is returned.

(3) $attrib = $Ct -> attrib ($pos, $len, $attrib)

When called in a scalar context with three parameters and $attrib is scalar, the attributes of Context Ct starting at position $pos with length $len will be set to $attrib.

(4) 1||undef = $Ct -> attrib ($o1, $l1, [@attrib], [@offset])

Substitutes attributes of Context Ct from position o1 and length l1 with attributes [@attrib] according to offsets [@offset].

(5) 1||undef = $Ct -> attrib ($o1, $l1, [@attrib], [@offset], $o2, $l2)

Like (4), but @attrib and @offset are reduced according to offset $o2 and length $l2.

(6) 1||undef = $Ct1 -> attrib ($o1, $l1, $Ct2)

Substitutes attributes of Context $Ct1 from position $o1 and length $l1 with attributes of Context Ct2 from position 0 to position $l1.

Note: Attrib does not care for the length of $Ct2!

(7) 1||undef = $Ct1 -> attrib ($o1, $l1, $Ct2 [,$o2, $l2])

Like (6), but only the part of $Ct2 from position $o2 with a length $l2 is used.

charsize

$Ct = $Ct -> charsize ([$bytesize_of_one_char])

Returns the character size of Context Ct. The character size is the size of one character measured in bytes. If parameter bytesize is given, Context Ct additionally is converted to a Context with the new character size bytesize.

chunks

[ [$str1, $attr1], [$str2, $attr2], ... ] = $Ct -> chunks ()

Until now this is the only way to traverse a Context by it's different attributes. You would use it like:

   for ( @{$Ct1->chunks()} ) {
      my ($text, $attrib) = @{$_};
      if ($attrib) {
         # do something (text has attribute $attrib)
      } else {
         # do something (text has default attribute)
      }
   }
clone

$Ct2 = $Ct1 -> clone

Returns a 1:1 copy of Context Ct1 as new Context Ct2.

dump

$Ct = $Ct -> dump

For debugging purposes. Dumps the Context structure to stdout.

eq

1||0 = $Ct1 -> eq ($Ct2)

Returns 1, if Context1 is equal to Context2, returns 0 otherwise.

index

$pos = $Ct -> index ($string [,$pos])

Analogue to perls index. (see "man perlfunc")

join

$Ct = Convert::Context -> join ($expr, $Ct1||$str1||$strR1, ...)

Concatenates all strings (scalar or reference) or Contexts with delimitor $expr, returns a new build Context.

$Ct1 = $Ct1 -> join ($Ct2||$str2||$strR2, ...)

Like above, but modifies $Ct1 instead of creating a new Context.

lc

$Ct2 = $Ct1 -> lc

Like perls lc. Returns a lowercased version of Context1 as Context2.

lcfirst

$Ct2 = $Ct1 -> lcfirst

Like perls lcfirst. Returns a version of Context1 with a lowercased first character as Context2.

length

$length = $Ct -> length

Returns the length of Context Ct. This is the length of the text part of Ct, measured in characters.

ne

1||0 = $Ct1 -> ne ($Ct2)

Returns 1, if Context1 is different from Context2, returns 0 otherwise.

new

$Ct = Convert::Context -> new ([$cs])

$Ct = Convert::Context -> new ([$cs,] \$txt [,[@a], [@o]])

$Ct = Convert::Context -> new ([$cs,] [\$txt [,[@a], [@o]]], [...], ...)

Returns a new Context string. It can be initialized three ways: (1) Without parameters, (2) with a reference to a text string, an attrib list reference and an offset list reference, or (3) with a list of references of (2).

Optionally it can be initialized with a leading parameter $cs. This stands for "character length" and specifies the byte size of one character. One needs this when using e.g. UTF16 (Unicode) characters.

Example:

 (1) 
   $Empty = Convert::Context -> new;

 (2) 
   $Plain = Convert::Context -> new (\("Plain text\n"));
   $Bold  = Convert::Context -> new (\("Attribute 1 text"), [1]);

 (3)
   Special (but useful) case:
   $Mixed = Convert::Context -> new (
      [\("This is an "),                         [0] ],
      [\("all bold"),                          [122] ],
      [\(", short and sometimes ")                   ],
      [\("italic"),       ["Strange text attribute"] ],
      [\(" text."                                    ]
   ;

Attribute 0 and Offset 0 is used as default value, if none is explicitly given. The meaning of all attributes (here 0, 122 and "Strange text attribute") has to be defined 100% by the applying code. In this example one would assume, that a text processor was connoting the attributes 0, 122 and "Strange text attribute" to the semantics: plain, bold and italic.

replace

$n = $Ct -> replace ($pattern, $replace, egimosx)

Replaces one or all occurrances matching to $pattern with $replace. Returns the number of replacements, or false if pattern is not found. Implemented mainly via perls replace operator:

   s/$pattern/$replace/egimosx

$replace here can be a string, a Context or a code reference. In the latter case this routine will be called at each match, passing the matched string as parameter. The matched text will then be replaced with the return value of the routine.

$n = $Ct -> replace ([@pattern], [@replace], egimosx)

You can call replace with list references holding corresponding sets of patterns and replacements. pattern and replace can be strings or Contexts, and replace additionally code references. The patterns will be glued together to a single pattern match, using pattern match or operator |.

Examples:

   (1) $Ct -> replace ("krims", "kram", "g")

Option g says, that not only one, but all occurrances of string "krims" shall be substituted by string "kram". "kram" will get the attributes of "krims" (see method "substr"). If you want to have more control about the attributes of "kram", you can pass the replacement string as a Context.

   (2) $Ct -> replace ("krims", $Ct, "g")

Replaces all occurrances of string "krims" with the Context $Ct. This is useful, if you want to have $Ct special attributes.

   (3) $Ct -> replace (" asta tu ", " AStA TU ", "ig")

Option i says, that the characters case shall be ignored. So example (3) would replace " asta tu ", " ASTA TU ", " Asta Tu " ... with " AStA TU ". (AStA stands for Allgemeiner Studierendenausschuß. Students governments are called like this in Germany and quite cool).

   (4) $Ct -> replace ("\02", \&footnote, "g")

This would call a function "footnote". The function will be called with three parameters:

   &function($match, $Ct, $pos)

   1. The matched string (here "\02")
   2. The Context        (here $Ct)
   3. The match position 

   (5) $Ct -> replace ("krims", sub {allow (@_, "kram")}, "ig")

This notation would call a function "allow" for each match, quite like (4). But further more here the string "kram" would be passed as additional parameter.

   (6) $Ct -> replace (["a", "o"], ["o", "a"], "g")

Substitutes a's with o's and o's with a's.

rindex

$pos = $Ct -> rindex ($string [,$pos])

Analogue to perls rindex. (try "man perlfunc")

split

@Ct = $Ct -> split ($pattern [$option [,$limit]])

Similar to perls split. Splits a Context according to string or Context delimitor $pattern. Returns an array of Contexts. If $limit is given, returns only that many elements. $pattern and $option are to be used according to perls s/$pattern/something/egimosx operator (but option "g" here is always set).

substr

$Ct2 = $Ct1 -> substr ($o1, $l1)

Returns a partial Context of Ct1 as new Context Ct2. Ct2 will be copied from Ct1 starting at position o1 and with the length l1.

$Ct = $Ct -> substr ($o1, $l1, $str [,$o2, $l2])

If a string is given as argument, the partial Context starting at offset o1 with length l1 is substituted by string. String gets the attributes of the partial Context. If e.g. the string to be replaced would be "<0>di<1>n<2>g<0>s", after the replacement it might look like "<0>bu<1>m<2>s".

$Ct1 = $Ct1 -> substr ($o1, $l1, $Ct2 [,$o2, $l2])

The partial Context of Ct1 starting at offset o1 with length l2 is substituted by Context Ct2.

If o<n> is undef, o<n> is set to 0.

If l<n> is undef, l<n> is set according to end of Ct<n>

text

\$text = $Ct -> text

Returns a reference to the text section of Context Ct.

tr

Synonyme for y. (see below)

uc

$Ct2 = $Ct1 -> uc

Like perls uc. Returns an uppercased version of Context Ct1 as Context Ct2.

ucfirst

$Ct2 = $Ct1 -> ucfirst

Like perls ucfirst. Returns a version of Context Ct1 with an uppercased first character as Context Ct2.

y

y can be called three ways.

(1) $Ct -> y ($search, $replace, cds)

If charsize of $Ct is 1, y behaves quite like perls y. Each character of string $search is replaced by the corresponding character of $replace.

(2) $Ct -> y ($search, $replace, egimosx)

If charsize of $Ct is bigger than 1, y breaks the strings $search and $replace into substrings, each charsize bytes long. These corresponding strings are then passed to method replace, automatically with option "g". Note, that thus internally perls tr operator is not used.

(3) $Ct -> y ([@search], [@replace], egimosx)

This actually just calls method replace with option "g". @search and @replace can be Contexts or strings, just like you want. @replace can also be code references.

ERRORS ^

- I just found that split with parameter $limit behaves not like perls split. Will be done.

TO DO ^

- Speeding up

- When programming this a long time ago I was very fond of references. Today this seems quite odd to me sometimes. So there might happen a major redesign sooner or later.

- The only real way to traverse a Context is still method "chunks()". There need to be some enhancements.

- Support for a hash parameter style for calling methods?

- Support for overloaded operators?

AUTHOR ^

Martin Schwartz <schwartz@cs.tu-berlin.de>.

syntax highlighting: