File: quick_icu.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (55 lines) | stat: -rw-r--r-- 1,777 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

// Copyright 1998-2002 John Maddock
// Copyright 2017 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt

// See library home page at http://www.boost.org/libs/regex

#include <boost/regex/icu.hpp>
#include <cassert>
#include <string>

bool validate_card_format(const std::string& s)
{
    static const boost::u32regex e = boost::make_u32regex("(\\d{4}[- ]){3}\\d{4}");
    return boost::u32regex_match(s, e);
}

const boost::u32regex card_rx = boost::make_u32regex("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z");
const std::string machine_format("\\1\\2\\3\\4");
const std::string human_format("\\1-\\2-\\3-\\4");

std::string machine_readable_card_number(const std::string& s)
{
    return boost::u32regex_replace(s, card_rx, machine_format, boost::match_default | boost::format_sed);
}

std::string human_readable_card_number(const std::string& s)
{
    return boost::u32regex_replace(s, card_rx, human_format, boost::match_default | boost::format_sed);
}

int main()
{
    std::string s[ 4 ] = { "0000111122223333", "0000 1111 2222 3333", "0000-1111-2222-3333", "000-1111-2222-3333" };

    assert( !validate_card_format( s[0] ) );
    assert( machine_readable_card_number( s[0] ) == s[0] );
    assert( human_readable_card_number( s[0] ) == s[2] );

    assert( validate_card_format( s[1] ) );
    assert( machine_readable_card_number( s[1] ) == s[0] );
    assert( human_readable_card_number( s[1] ) == s[2] );

    assert( validate_card_format( s[2] ) );
    assert( machine_readable_card_number( s[2] ) == s[0] );
    assert( human_readable_card_number( s[2] ) == s[2] );

    assert( !validate_card_format( s[3] ) );

    return 0;
}