Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
NumberFormatter.c File Reference
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "NumberFormatter.h"

Go to the source code of this file.

Macros

#define _Class   _NumberFormatter
 

Functions

Class_NumberFormatter (void)
 
static void initialize (Class *clazz)
 
static NumberFormatterinitWithFormat (NumberFormatter *self, const char *fmt)
 
static NumbernumberFromString (const NumberFormatter *self, const String *string)
 
static StringstringFromNumber (const NumberFormatter *self, const Number *number)
 

Macro Definition Documentation

◆ _Class

#define _Class   _NumberFormatter

Definition at line 30 of file NumberFormatter.c.

Function Documentation

◆ _NumberFormatter()

Class * _NumberFormatter ( void  )

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.
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ initialize()

static void initialize ( Class clazz)
static
See also
Class::initialize(Class *)

Definition at line 80 of file NumberFormatter.c.

80 {
81
82 ((NumberFormatterInterface *) clazz->interface)->numberFromString = numberFromString;
83 ((NumberFormatterInterface *) clazz->interface)->initWithFormat = initWithFormat;
84 ((NumberFormatterInterface *) clazz->interface)->stringFromNumber = stringFromNumber;
85}
ident interface
The interface of the Class.
Definition: Class.h:105
DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
Initializes a DateFormatter with the specified format string.
Definition: DateFormatter.c:76
String * stringFromNumber(const NumberFormatter *self, const Number *number)
Yields a String representation of the specified Number instance.
Number * numberFromString(const NumberFormatter *self, const String *string)
Parses a Number from the specified String.

◆ initWithFormat()

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

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.
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
const char * fmt
The format string.
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ numberFromString()

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

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
MutableString * string(void)
Returns a new MutableString.
A wrapper for placing numeric primitives into collections, etc.
Definition: Number.h:41
Number * initWithValue(Number *self, double value)
Initializes this Number with the specified value.
Definition: Number.c:137
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85

◆ stringFromNumber()

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

Definition at line 70 of file NumberFormatter.c.

70 {
71
72 return $(alloc(String), initWithFormat, self->fmt, number->value);
73}
double value
The backing value.
Definition: Number.h:57
Immutable UTF-8 strings.
Definition: String.h:69