All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CHAR_FRAGMENT Class Reference

#include <unicharset.h>

Public Member Functions

void set_all (const char *unichar, int pos, int total, bool natural)
 
void set_unichar (const char *uch)
 
void set_pos (int p)
 
void set_total (int t)
 
const char * get_unichar () const
 
int get_pos () const
 
int get_total () const
 
STRING to_string () const
 
bool equals (const char *other_unichar, int other_pos, int other_total) const
 
bool equals (const CHAR_FRAGMENT *other) const
 
bool is_continuation_of (const CHAR_FRAGMENT *fragment) const
 
bool is_beginning () const
 
bool is_ending () const
 
bool is_natural () const
 
void set_natural (bool value)
 

Static Public Member Functions

static STRING to_string (const char *unichar, int pos, int total, bool natural)
 
static CHAR_FRAGMENTparse_from_string (const char *str)
 

Static Public Attributes

static const int kMinLen = 6
 
static const int kMaxLen = 3 + UNICHAR_LEN + 2
 
static const int kMaxChunks = 5
 

Detailed Description

Definition at line 42 of file unicharset.h.

Member Function Documentation

bool CHAR_FRAGMENT::equals ( const char *  other_unichar,
int  other_pos,
int  other_total 
) const
inline

Definition at line 79 of file unicharset.h.

80  {
81  return (strcmp(this->unichar, other_unichar) == 0 &&
82  this->pos == other_pos && this->total == other_total);
83  }
bool CHAR_FRAGMENT::equals ( const CHAR_FRAGMENT other) const
inline

Definition at line 84 of file unicharset.h.

84  {
85  return this->equals(other->get_unichar(),
86  other->get_pos(),
87  other->get_total());
88  }
int get_total() const
Definition: unicharset.h:66
bool equals(const char *other_unichar, int other_pos, int other_total) const
Definition: unicharset.h:79
const char * get_unichar() const
Definition: unicharset.h:64
int get_pos() const
Definition: unicharset.h:65
int CHAR_FRAGMENT::get_pos ( ) const
inline

Definition at line 65 of file unicharset.h.

65 { return this->pos; }
int CHAR_FRAGMENT::get_total ( ) const
inline

Definition at line 66 of file unicharset.h.

66 { return this->total; }
const char* CHAR_FRAGMENT::get_unichar ( ) const
inline

Definition at line 64 of file unicharset.h.

64 { return this->unichar; }
bool CHAR_FRAGMENT::is_beginning ( ) const
inline

Definition at line 99 of file unicharset.h.

99 { return this->pos == 0; }
bool CHAR_FRAGMENT::is_continuation_of ( const CHAR_FRAGMENT fragment) const
inline

Definition at line 92 of file unicharset.h.

92  {
93  return (strcmp(this->unichar, fragment->get_unichar()) == 0 &&
94  this->total == fragment->get_total() &&
95  this->pos == fragment->get_pos() + 1);
96  }
int get_total() const
Definition: unicharset.h:66
const char * get_unichar() const
Definition: unicharset.h:64
int get_pos() const
Definition: unicharset.h:65
bool CHAR_FRAGMENT::is_ending ( ) const
inline

Definition at line 102 of file unicharset.h.

102 { return this->pos == this->total-1; }
bool CHAR_FRAGMENT::is_natural ( ) const
inline

Definition at line 107 of file unicharset.h.

107 { return natural; }
CHAR_FRAGMENT * CHAR_FRAGMENT::parse_from_string ( const char *  str)
static

Definition at line 1038 of file unicharset.cpp.

1038  {
1039  const char *ptr = string;
1040  int len = strlen(string);
1041  if (len < kMinLen || *ptr != kSeparator) {
1042  return NULL; // this string can not represent a fragment
1043  }
1044  ptr++; // move to the next character
1045  int step = 0;
1046  while ((ptr + step) < (string + len) && *(ptr + step) != kSeparator) {
1047  step += UNICHAR::utf8_step(ptr + step);
1048  }
1049  if (step == 0 || step > UNICHAR_LEN) {
1050  return NULL; // no character for unichar or the character is too long
1051  }
1052  char unichar[UNICHAR_LEN + 1];
1053  strncpy(unichar, ptr, step);
1054  unichar[step] = '\0'; // null terminate unichar
1055  ptr += step; // move to the next fragment separator
1056  int pos = 0;
1057  int total = 0;
1058  bool natural = false;
1059  char *end_ptr = NULL;
1060  for (int i = 0; i < 2; i++) {
1061  if (ptr > string + len || *ptr != kSeparator) {
1062  if (i == 1 && *ptr == kNaturalFlag)
1063  natural = true;
1064  else
1065  return NULL; // Failed to parse fragment representation.
1066  }
1067  ptr++; // move to the next character
1068  i == 0 ? pos = static_cast<int>(strtol(ptr, &end_ptr, 10))
1069  : total = static_cast<int>(strtol(ptr, &end_ptr, 10));
1070  ptr = end_ptr;
1071  }
1072  if (ptr != string + len) {
1073  return NULL; // malformed fragment representation
1074  }
1075  CHAR_FRAGMENT *fragment = new CHAR_FRAGMENT();
1076  fragment->set_all(unichar, pos, total, natural);
1077  return fragment;
1078 }
static int utf8_step(const char *utf8_str)
Definition: unichar.cpp:134
static const int kMinLen
Definition: unicharset.h:45
#define NULL
Definition: host.h:144
#define UNICHAR_LEN
Definition: unichar.h:30
void set_all(const char *unichar, int pos, int total, bool natural)
Definition: unicharset.h:52
void CHAR_FRAGMENT::set_all ( const char *  unichar,
int  pos,
int  total,
bool  natural 
)
inline

Definition at line 52 of file unicharset.h.

52  {
53  set_unichar(unichar);
54  set_pos(pos);
55  set_total(total);
56  set_natural(natural);
57  }
void set_pos(int p)
Definition: unicharset.h:62
void set_total(int t)
Definition: unicharset.h:63
void set_natural(bool value)
Definition: unicharset.h:108
void set_unichar(const char *uch)
Definition: unicharset.h:58
void CHAR_FRAGMENT::set_natural ( bool  value)
inline

Definition at line 108 of file unicharset.h.

108 { natural = value; }
void CHAR_FRAGMENT::set_pos ( int  p)
inline

Definition at line 62 of file unicharset.h.

62 { this->pos = p; }
void CHAR_FRAGMENT::set_total ( int  t)
inline

Definition at line 63 of file unicharset.h.

63 { this->total = t; }
void CHAR_FRAGMENT::set_unichar ( const char *  uch)
inline

Definition at line 58 of file unicharset.h.

58  {
59  strncpy(this->unichar, uch, UNICHAR_LEN);
60  this->unichar[UNICHAR_LEN] = '\0';
61  }
#define UNICHAR_LEN
Definition: unichar.h:30
STRING CHAR_FRAGMENT::to_string ( const char *  unichar,
int  pos,
int  total,
bool  natural 
)
static

Definition at line 1025 of file unicharset.cpp.

1026  {
1027  if (total == 1) return STRING(unichar);
1028  STRING result = "";
1029  result += kSeparator;
1030  result += unichar;
1031  char buffer[kMaxLen];
1032  snprintf(buffer, kMaxLen, "%c%d%c%d", kSeparator, pos,
1033  natural ? kNaturalFlag : kSeparator, total);
1034  result += buffer;
1035  return result;
1036 }
static const int kMaxLen
Definition: unicharset.h:47
Definition: strngs.h:44
STRING CHAR_FRAGMENT::to_string ( ) const
inline

Definition at line 73 of file unicharset.h.

73  {
74  return to_string(unichar, pos, total, natural);
75  }
STRING to_string() const
Definition: unicharset.h:73

Member Data Documentation

const int CHAR_FRAGMENT::kMaxChunks = 5
static

Definition at line 49 of file unicharset.h.

const int CHAR_FRAGMENT::kMaxLen = 3 + UNICHAR_LEN + 2
static

Definition at line 47 of file unicharset.h.

const int CHAR_FRAGMENT::kMinLen = 6
static

Definition at line 45 of file unicharset.h.


The documentation for this class was generated from the following files: