Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
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:86
static void initialize(Class *clazz)
Class * _Object(void)
Definition Object.c:136
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.

◆ 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}
static Number * numberFromString(const NumberFormatter *self, const String *string)
static String * stringFromNumber(const NumberFormatter *self, const Number *number)
static NumberFormatter * initWithFormat(NumberFormatter *self, const char *fmt)
ident interface
The interface of the Class.
Definition Class.h:105
String * stringFromNumber(const NumberFormatter *self, const Number *number)
Yields a String representation of the specified Number instance.

◆ 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}
static Array * init(Array *self)
Definition Array.c:420
#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

◆ 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:176
static Number * initWithValue(Number *self, const double value)
Definition Number.c:129
static String * string(void)
Definition String.c:905
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()

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
UTF-8 strings.
Definition String.h:69