unbrace 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/README.md +29 -0
- data/Rakefile +4 -0
- data/ext/unbrace/extconf.rb +3 -0
- data/ext/unbrace/unbrace.c +86 -0
- data/test/test.rb +17 -0
- data/unbrace.gemspec +19 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5c5f1b2af0a18d498126a5aaec25c2a8310f5f5e527102162d32ddd4b2cce49d
|
4
|
+
data.tar.gz: 98eed7afd2b05a27ef189d12429e5942e4c9e849dd3692b4c2238b09a520d65d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d93760f4f5a65918966282cfa755dbe6f5c9ddfc76d9fa2b78b828f4bd6df0585de4ad0ced9bdfbd43b420f6331c49078d8da345f8d1e2bef011bbcd064a6f13
|
7
|
+
data.tar.gz: 3ac1a167f444665e8f59e0697593bcaa5758b2acaf1d98f811f9ed5284df04072ec0870c1433012c24ac6020a2cc48333fd3eb2e45a7b52e631158f3facdd523
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# unbrace
|
2
|
+
|
3
|
+
```unbrace``` is a Ruby gem to expand braces, similar to a file glob.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Just call ```String#unbrace``` for any string containing embeded braces to expand.
|
8
|
+
|
9
|
+
## Example
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
irb> puts "lawyer_{name,{work,home}_email_{active,inactive},state}".unbrace
|
13
|
+
lawyer_name
|
14
|
+
lawyer_work_email_active
|
15
|
+
lawyer_work_email_inactive
|
16
|
+
lawyer_home_email_active
|
17
|
+
lawyer_home_email_inactive
|
18
|
+
lawyer_state
|
19
|
+
```
|
20
|
+
|
21
|
+
## Test
|
22
|
+
|
23
|
+
```
|
24
|
+
ruby -runbrace test/test.rb
|
25
|
+
```
|
26
|
+
|
27
|
+
## License
|
28
|
+
|
29
|
+
This software is licensed under terms of the MIT License.
|
data/Rakefile
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
// ============================================================================
|
2
|
+
// Summary: unbrace.c - Offers String#unbrace, which expands braces in strings.
|
3
|
+
// Version: 0.5.0
|
4
|
+
// Author: Steve Shreeve (steve.shreeve@gmail.com)
|
5
|
+
// Date: December 16, 2023
|
6
|
+
// Legal: Same license as Ruby (see http://www.ruby-lang.org/en/LICENSE.txt)
|
7
|
+
// Note: This file is derived from "dir.c" in the Ruby source code.
|
8
|
+
// ============================================================================
|
9
|
+
|
10
|
+
#include "ruby/ruby.h"
|
11
|
+
// FIXME: Encoding requires(?) others, such as #include "ruby/encoding.h"
|
12
|
+
|
13
|
+
static int
|
14
|
+
brace_expand(const char *str, VALUE ary) // FIXME: Add ",rb_encoding *enc)" here?
|
15
|
+
{
|
16
|
+
const char *p = str;
|
17
|
+
const char *pend = p + strlen(p);
|
18
|
+
const char *s = p;
|
19
|
+
const char *lbrace = 0, *rbrace = 0;
|
20
|
+
int nest = 0, status = 0;
|
21
|
+
|
22
|
+
while (*p) {
|
23
|
+
if (*p == '{' && nest++ == 0) {
|
24
|
+
lbrace = p;
|
25
|
+
}
|
26
|
+
if (*p == '}' && lbrace && --nest == 0) {
|
27
|
+
rbrace = p;
|
28
|
+
break;
|
29
|
+
}
|
30
|
+
if (*p == '\\' && !*++p) {
|
31
|
+
break;
|
32
|
+
}
|
33
|
+
p++; // FIXME: For encoding, use Inc(p, pend, enc) here?
|
34
|
+
}
|
35
|
+
|
36
|
+
if (lbrace && rbrace) {
|
37
|
+
size_t len = strlen(s) + 1;
|
38
|
+
char *buf = (char *) malloc(len); // FIXME: Use ALLOC_N(...) here?
|
39
|
+
long shift;
|
40
|
+
|
41
|
+
if (!buf) return -1;
|
42
|
+
memcpy(buf, s, lbrace - s);
|
43
|
+
shift = (lbrace - s);
|
44
|
+
p = lbrace;
|
45
|
+
while (p < rbrace) {
|
46
|
+
const char *t = ++p;
|
47
|
+
nest = 0;
|
48
|
+
while (p < rbrace && !(*p == ',' && nest == 0)) {
|
49
|
+
if (*p == '{') nest++;
|
50
|
+
if (*p == '}') nest--;
|
51
|
+
if (*p == '\\' && (++p == rbrace)) break;
|
52
|
+
p++; // FIXME: For encoding, use Inc(p, pend, enc) here?
|
53
|
+
}
|
54
|
+
memcpy(buf + shift, t, p - t);
|
55
|
+
strlcpy(buf + shift + (p - t), rbrace + 1, len - (shift + (p - t)));
|
56
|
+
status = brace_expand(buf, ary);
|
57
|
+
}
|
58
|
+
free(buf); // FIXME: Use GLOB_FREE(buf) here?
|
59
|
+
}
|
60
|
+
else if (!lbrace && !rbrace) {
|
61
|
+
rb_ary_push(ary, rb_str_new_cstr(str));
|
62
|
+
status = 1;
|
63
|
+
}
|
64
|
+
|
65
|
+
return status;
|
66
|
+
}
|
67
|
+
|
68
|
+
static VALUE
|
69
|
+
rb_str_unbrace(VALUE str)
|
70
|
+
{
|
71
|
+
if (!rb_block_given_p()) {
|
72
|
+
VALUE ary = rb_ary_new();
|
73
|
+
brace_expand(RSTRING_PTR(str), ary);
|
74
|
+
return ary;
|
75
|
+
}
|
76
|
+
else {
|
77
|
+
return Qnil;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
void
|
82
|
+
Init_unbrace(void)
|
83
|
+
{
|
84
|
+
VALUE rb_cString = rb_define_class("String", rb_cObject);
|
85
|
+
rb_define_method(rb_cString, "unbrace", rb_str_unbrace, 0);
|
86
|
+
}
|
data/test/test.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "unbrace"
|
2
|
+
|
3
|
+
strs = <<'EOS'
|
4
|
+
1{2,{3,4}5{6,7},8}{9,{a,b,c},d}e
|
5
|
+
a{b,{c,d}_e_{f,g},h}{i,{j,k,l},m}n
|
6
|
+
lawyer_{name,{work,home}_email_{active,inactive},state}
|
7
|
+
~/{Downloads,Pictures}/*.{jpg,gif,png}
|
8
|
+
It{{em,alic}iz,erat}e{d,}, please.
|
9
|
+
{,{,gotta have{ ,\, again\, }}more }cowbell!
|
10
|
+
{}} some }{,{\\{ edge, edge} \,}{ cases, {here} \\\\\}
|
11
|
+
EOS
|
12
|
+
|
13
|
+
strs.each_line do |s|
|
14
|
+
puts s.chomp!
|
15
|
+
puts s.unbrace
|
16
|
+
puts
|
17
|
+
end
|
data/unbrace.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "unbrace"
|
5
|
+
s.version = `grep -m 1 '^// Version:' ext/unbrace/unbrace.c | cut -f 3 -d ' '`
|
6
|
+
s.author = "Steve Shreeve"
|
7
|
+
s.email = "steve.shreeve@gmail.com"
|
8
|
+
s.summary = "A Ruby gem that expands braces in strings"
|
9
|
+
s.description = "Expand braces, similar to a file glob"
|
10
|
+
s.homepage = "https://github.com/shreeve/unbrace"
|
11
|
+
s.license = "MIT"
|
12
|
+
s.platform = Gem::Platform::RUBY
|
13
|
+
s.files = `git ls-files`.split("\n") - %w[.gitignore]
|
14
|
+
s.extensions << 'ext/unbrace/extconf.rb'
|
15
|
+
s.add_development_dependency "bundler", "~> 2.0"
|
16
|
+
s.add_development_dependency "rake", "~> 13.1"
|
17
|
+
s.add_development_dependency "rake-compiler", "~> 1.2"
|
18
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unbrace
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Shreeve
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake-compiler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.2'
|
55
|
+
description: Expand braces, similar to a file glob
|
56
|
+
email: steve.shreeve@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions:
|
59
|
+
- ext/unbrace/extconf.rb
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- Gemfile
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- ext/unbrace/extconf.rb
|
66
|
+
- ext/unbrace/unbrace.c
|
67
|
+
- test/test.rb
|
68
|
+
- unbrace.gemspec
|
69
|
+
homepage: https://github.com/shreeve/unbrace
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubygems_version: 3.4.22
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: A Ruby gem that expands braces in strings
|
92
|
+
test_files: []
|