Develop and Download Open Source Software

Browse Subversion Repository

Diff of /trunk/TTProxy/YCL/include/YCL/Integer.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3226 by maya, Tue Mar 24 09:37:20 2009 UTC revision 3227 by maya, Tue Mar 24 15:10:33 2009 UTC
# Line 1  Line 1 
1  /*  /*
2   * $Id: Integer.h,v 1.4 2007-08-18 08:52:18 maya Exp $   * $Id: Integer.h,v 1.4 2007-08-18 08:52:18 maya Exp $
3   */   */
4    
5  #ifndef _YCL_INTEGER_H_  #ifndef _YCL_INTEGER_H_
6  #define _YCL_INTEGER_H_  #define _YCL_INTEGER_H_
7    
8  #if _MSC_VER >= 1000  #if _MSC_VER >= 1000
9  #pragma once  #pragma once
10  #endif // _MSC_VER >= 1000  #endif // _MSC_VER >= 1000
11    
12  #include <YCL/String.h>  #include <YCL/String.h>
13    
14  namespace yebisuya {  namespace yebisuya {
15    
16  // intをラップするためのクラス。  // intをラップするためのクラス。
17  class Integer {  class Integer {
18  private:  private:
19          int value;          int value;
20  public:  public:
21          // デフォルトコンストラクタ          // デフォルトコンストラクタ
22          Integer():value(0){}          Integer():value(0){}
23          // 初期値付きコンストラクタ          // 初期値付きコンストラクタ
24          Integer(int value):value(value){}          Integer(int value):value(value){}
25          // キャスト演算子          // キャスト演算子
26          operator int()const {          operator int()const {
27                  return value;                  return value;
28          }          }
29          static String toString(long value) {          static String toString(long value) {
30                  return toString(value, 10);                  return toString(value, 10);
31          }          }
32          static String toString(long value, int base) {          static String toString(long value, int base) {
33                  bool negative = false;                  bool negative = false;
34                  if (value < 0) {                  if (value < 0) {
35                          negative = true;                          negative = true;
36                          value = -value;                          value = -value;
37                  }                  }
38                  return toString(value, 10, negative);                  return toString(value, 10, negative);
39          }          }
40          static String toString(unsigned long value) {          static String toString(unsigned long value) {
41                  return toString(value, 10);                  return toString(value, 10);
42          }          }
43          static String toString(unsigned long value, int base) {          static String toString(unsigned long value, int base) {
44                  return toString(value, base, false);                  return toString(value, base, false);
45          }          }
46          static String toString(unsigned long value, int base, bool negative) {          static String toString(unsigned long value, int base, bool negative) {
47                  if (base < 2 || base > 36)                  if (base < 2 || base > 36)
48                          return NULL;                          return NULL;
49                  char buffer[64];                  char buffer[64];
50                  char* p = buffer + countof(buffer);                  char* p = buffer + countof(buffer);
51                  *--p = '\0';                  *--p = '\0';
52                  if (value == 0) {                  if (value == 0) {
53                          *--p = '0';                          *--p = '0';
54                  }else{                  }else{
55                          while (value > 0) {                          while (value > 0) {
56                                  int d = value % base;                                  int d = value % base;
57                                  value /= base;                                  value /= base;
58                                  *--p = (d < 10 ? ('0' + d) : ('A' + d - 10));                                  *--p = (d < 10 ? ('0' + d) : ('A' + d - 10));
59                          }                          }
60                          if (negative) {                          if (negative) {
61                                  *--p = '-';                                  *--p = '-';
62                          }                          }
63                  }                  }
64                  return p;                  return p;
65          }          }
66          static long parseInt(const char* string) {          static long parseInt(const char* string) {
67                  return parseInt(string, 0);                  return parseInt(string, 0);
68          }          }
69          static long parseInt(const char* string, int base) {          static long parseInt(const char* string, int base) {
70                  if (base != 0 && base < 2 || base > 36)                  if (base != 0 && base < 2 || base > 36)
71                          return 0;                          return 0;
72                  long v = 0;                  long v = 0;
73                  bool negative = false;                  bool negative = false;
74                  const char* p = string;                  const char* p = string;
75                  // 空白のスキップ                  // 空白のスキップ
76                  while ('\0' < *p && *p <= ' ')                  while ('\0' < *p && *p <= ' ')
77                          p++;                          p++;
78                  if (*p == '-') {                  if (*p == '-') {
79                          negative = true;                          negative = true;
80                          p++;                          p++;
81                  }else if (*p == '+') {                  }else if (*p == '+') {
82                          p++;                          p++;
83                  }                  }
84                  // 空白のスキップ                  // 空白のスキップ
85                  while ('\0' < *p && *p <= ' ')                  while ('\0' < *p && *p <= ' ')
86                          p++;                          p++;
87                  // 基数の変更                  // 基数の変更
88                  if (base == 0) {                  if (base == 0) {
89                          if (*p == '0') {                          if (*p == '0') {
90                                  p++;                                  p++;
91                                  if (*p == 'x' || *p == 'X') {                                  if (*p == 'x' || *p == 'X') {
92                                          p++;                                          p++;
93                                          base = 16;                                          base = 16;
94                                  }else{                                  }else{
95                                          base = 8;                                          base = 8;
96                                  }                                  }
97                          }else{                          }else{
98                                  base = 10;                                  base = 10;
99                          }                          }
100                  }                  }
101                  while (*p != '\0') {                  while (*p != '\0') {
102                          int d;                          int d;
103                          if ('0' <= *p && *p <= '9') {                          if ('0' <= *p && *p <= '9') {
104                                  d = *p - '0';                                  d = *p - '0';
105                          }else if ('A' <= *p && *p <= 'Z') {                          }else if ('A' <= *p && *p <= 'Z') {
106                                  d = *p - 'A' + 10;                                  d = *p - 'A' + 10;
107                          }else if ('a' <= *p && *p <= 'z') {                          }else if ('a' <= *p && *p <= 'z') {
108                                  d = *p - 'a' + 10;                                  d = *p - 'a' + 10;
109                          }else{                          }else{
110                                  // 余計な文字が見つかれば終了                                  // 余計な文字が見つかれば終了
111                                  break;                                  break;
112                          }                          }
113                          // 基数以上だった場合は終了                          // 基数以上だった場合は終了
114                          if (d >= base)                          if (d >= base)
115                                  break;                                  break;
116                          v = v * base + d;                          v = v * base + d;
117                          // オーバーフローした場合は終了                          // オーバーフローした場合は終了
118                          if (v < 0)                          if (v < 0)
119                                  break;                                  break;
120                          p++;                          p++;
121                  }                  }
122                  if (negative) {                  if (negative) {
123                          v = -v;                          v = -v;
124                  }                  }
125                  return v;                  return v;
126          }          }
127  };  };
128    
129  }  }
130    
131  #endif//_YCL_INTEGER_H_  #endif//_YCL_INTEGER_H_

Legend:
Removed from v.3226  
changed lines
  Added in v.3227

SourceForge.JP is a Japanese version of SourceForge.net. For developments that are not related to Japan, we recommend you to use SourceForge.net.