Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
NumberFormatter Struct Reference

#include <NumberFormatter.h>

Overview

Number formatting and parsing.

Definition at line 57 of file NumberFormatter.h.

Inheritance diagram for NumberFormatter:
Object

Properties

const char * fmt
 The format string. More...
 
Object object
 The superclass. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_NumberFormatter (void)
 The NumberFormatter archetype. More...
 
NumberFormatterinitWithFormat (NumberFormatter *self, const char *fmt)
 Initializes a NumberFormatter with the specified format string. More...
 
NumbernumberFromString (const NumberFormatter *self, const String *string)
 Parses a Number from the specified String. More...
 
StringstringFromNumber (const NumberFormatter *self, const Number *number)
 Yields a String representation of the specified Number instance. More...
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype. More...
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object. More...
 
void dealloc (Object *self)
 Frees all resources held by this Object. More...
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object. More...
 
_Bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object. More...
 
_Bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership. More...
 

Protected Attributes

NumberFormatterInterface * interface
 The interface. More...
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface. More...
 

Property Details

◆ fmt

const char* NumberFormatter::fmt

The format string.

Definition at line 73 of file NumberFormatter.h.

◆ interface

NumberFormatterInterface* NumberFormatter::interface
protected

The interface.

Definition at line 68 of file NumberFormatter.h.

◆ object

Object NumberFormatter::object

The superclass.

Definition at line 62 of file NumberFormatter.h.

Method Details

◆ _NumberFormatter()

Class * _NumberFormatter ( void  )

The NumberFormatter archetype.

Returns
The NumberFormatter Class.

Definition at line 91 of file NumberFormatter.c.

91 {
92 static Class *clazz;
93 static Once once;
94
95 do_once(&once, {
96 clazz = _initialize(&(const ClassDef) {
97 .name = "NumberFormatter",
98 .superclass = _Object(),
99 .instanceSize = sizeof(NumberFormatter),
100 .interfaceOffset = offsetof(NumberFormatter, interface),
101 .interfaceSize = sizeof(NumberFormatterInterface),
103 });
104 });
105
106 return clazz;
107}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
Number formatting and parsing.
NumberFormatterInterface * interface
The interface.
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ initWithFormat()

NumberFormatter * initWithFormat ( NumberFormatter self,
const char *  fmt 
)

Initializes a NumberFormatter with the specified format string.

Parameters
selfThe NumberFormatter.
fmtThe format string.
Returns
The initialized NumberFormatter, or NULL on error.
See also
strftime(3)

Definition at line 38 of file NumberFormatter.c.

38 {
39
40 self = (NumberFormatter *) super(Object, self, init);
41 if (self) {
42 self->fmt = fmt ?: NUMBERFORMAT_DECIMAL;
43 }
44
45 return self;
46}
#define super(type, obj, method,...)
#define NUMBERFORMAT_DECIMAL
Decimal format.
const char * fmt
The format string.
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Object * init(Object *self)
Initializes this Object.
Definition: Object.c:83

◆ numberFromString()

Number * numberFromString ( const NumberFormatter self,
const String string 
)

Parses a Number from the specified String.

Parameters
selfThe NumberFormatter.
stringThe String to parse.
Returns
A Number instance, or NULL on error.

Definition at line 52 of file NumberFormatter.c.

52 {
53
54 if (string) {
55 double value;
56
57 const int res = sscanf(string->chars, self->fmt, &value);
58 if (res == 1) {
59 return $(alloc(Number), initWithValue, value);
60 }
61 }
62
63 return NULL;
64}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
static MutableString * string(void)
static Number * initWithValue(Number *self, const double value)
Definition: Number.c:137
A wrapper for placing numeric primitives into collections, etc.
Definition: Number.h:41
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85

◆ stringFromNumber()

String * stringFromNumber ( const NumberFormatter self,
const Number number 
)

Yields a String representation of the specified Number instance.

Parameters
selfThe NumberFormatter.
numberThe Number to format.
Returns
The String representation of the Number, or NULL on error.

Definition at line 70 of file NumberFormatter.c.

70 {
71
72 return $(alloc(String), initWithFormat, self->fmt, number->value);
73}
NumberFormatter * initWithFormat(NumberFormatter *self, const char *fmt)
Initializes a NumberFormatter with the specified format string.
double value
The backing value.
Definition: Number.h:57
Immutable UTF-8 strings.
Definition: String.h:69

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