├── go.mod ├── .gitignore ├── README.md ├── uuid4.go ├── rand_test.go ├── uuid4_test.go ├── rand.go └── LICENSE /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/lifei6671/gorand 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gorand 2 | 3 | Golang 随机字符串生成库 4 | 5 | ## 使用 6 | 7 | ```bash 8 | go get github.com/lifei6671/gorand 9 | ``` 10 | 11 | ## 实例 12 | 13 | ```go 14 | //生成指定长度的字符串,使用 Apache 的算法 15 | gorand.RandomAlphabetic(10) 16 | 17 | //使用随机数算法生成字符串 18 | gorand.KRand(20, KC_RAND_KIND_ALL) 19 | 20 | //生成 UUID4 字符串 21 | gorand.NewUUID4().String() 22 | ``` 23 | -------------------------------------------------------------------------------- /uuid4.go: -------------------------------------------------------------------------------- 1 | // Package uuid4 provides functions for generating and parsing uuids 2 | // compliant with RFC 4122 3 | // https://github.com/frankenbeanies/uuid4/ 4 | package gorand 5 | 6 | import ( 7 | "crypto/rand" 8 | "encoding/hex" 9 | "errors" 10 | "strings" 11 | ) 12 | 13 | // UUID4 is a container for an RFC 4122 compliant uuid 14 | type UUID4 struct { 15 | bytes []byte 16 | } 17 | 18 | // New generates a new RFC 4122 compliant uuid 19 | func NewUUID4() *UUID4 { 20 | bytes := make([]byte, 16) 21 | if _, err := rand.Read(bytes); err != nil { 22 | panic(err.Error()) 23 | } 24 | 25 | bytes[6] = byte(0x40 | (int(bytes[6]) & 0xf)) 26 | bytes[8] = byte(0x80 | (int(bytes[8]) & 0x3f)) 27 | 28 | return &UUID4{bytes: bytes} 29 | } 30 | 31 | // String provides the uuid in a format compliant with RFC 4122 32 | func (uuid *UUID4) String() string { 33 | str := hex.EncodeToString(uuid.bytes) 34 | 35 | return str[:8] + "-" + str[8:12] + "-" + str[12:16] + "-" + str[16:20] + "-" + str[20:] 36 | } 37 | 38 | // Bytes provides the bytes of the uuid 39 | func (uuid *UUID4) Bytes() []byte { 40 | val := make([]byte, 16) 41 | copy(val, uuid.bytes) 42 | return val 43 | } 44 | 45 | // ParseString parses a RFC 4122 compliant string representation of a uuid into a UUID4 46 | func ParseString(str string) (uuid *UUID4, err error) { 47 | noDash := strings.Replace(str, "-", "", -1) 48 | noDash = strings.ToLower(noDash) 49 | 50 | if len(noDash) != 32 { 51 | return nil, errors.New(str + " is not a valid UUID4. The unhyphenated string representation should be 32 characters in length") 52 | } 53 | 54 | if noDash[12] != '4' { 55 | return nil, errors.New(str + " is not a valid UUID4. character 13 should be '4'.") 56 | } 57 | 58 | if noDash[16] != '8' && noDash[16] != '9' && noDash[16] != 'a' && noDash[16] != 'b' { 59 | return nil, errors.New(str + " is not a valid UUID4. character 17 should be '8', '9', 'a', or 'b'") 60 | } 61 | 62 | bytes := make([]byte, 18) 63 | bytes, err = hex.DecodeString(noDash) 64 | 65 | if err != nil { 66 | return nil, err 67 | } 68 | 69 | return &UUID4{bytes: bytes}, nil 70 | } 71 | -------------------------------------------------------------------------------- /rand_test.go: -------------------------------------------------------------------------------- 1 | package gorand 2 | 3 | import ( 4 | "fmt" 5 | "regexp" 6 | "testing" 7 | ) 8 | 9 | func assertTrue(t *testing.T, expr bool, arg1 string, arg2 string) { 10 | if expr { 11 | t.Log(arg1) 12 | } else { 13 | t.Error(arg2) 14 | } 15 | } 16 | 17 | func assertEquals(t *testing.T, arg1, arg2 interface{}, falseMsg string, params ...interface{}) { 18 | if arg1 == arg2 { 19 | t.Log("PASS") 20 | } else { 21 | if len(params) == 0 { 22 | t.Error(falseMsg) 23 | } else { 24 | t.Error(falseMsg, params) 25 | } 26 | } 27 | } 28 | 29 | func TestRandomString(t *testing.T) { 30 | 31 | // random utf8 string 32 | str1 := RandomString(21) 33 | assertEquals(t, len([]rune(str1)), 21, "RandomString(21) length") 34 | 35 | str2 := RandomString(21) 36 | assertEquals(t, len([]rune(str2)), 21, "RandomString(21) length") 37 | assertTrue(t, func() bool { 38 | str1Rune := []rune(str1) 39 | str2Rune := []rune(str2) 40 | for i := 0; i < len(str1Rune); i++ { 41 | if str1Rune[i] != str2Rune[i] { 42 | return false 43 | } 44 | } 45 | return true 46 | }(), "PASS", "str1 != str2") 47 | 48 | // random ascii 49 | str1 = RandomAscii(21) 50 | assertEquals(t, len(str1), 21, "RandomAscii(21) length") 51 | for _, r := range []rune(str1) { 52 | assertTrue(t, r >= 32 && r <= 127, "PASS", "char between 32 and 127") 53 | } 54 | str2 = RandomAscii(21) 55 | assertTrue(t, str1 != str2, "PASS", "str1 != str2") 56 | fmt.Printf("RandomAscii(21):\nstr1=%s\nstr2=%s\n", str1, str2) 57 | 58 | // random alphabetic 59 | str1 = RandomAlphabetic(21) 60 | assertEquals(t, len(str1), 21, "RandomAlphabetic(21) length") 61 | assertTrue(t, func() bool { 62 | str1Rune := []rune(str1) 63 | all_alphabetic := true 64 | for i := 0; i < len(str1Rune); i++ { 65 | ch := str1Rune[i] 66 | if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') { 67 | continue 68 | } else { 69 | all_alphabetic = false 70 | break 71 | } 72 | } 73 | return all_alphabetic 74 | }(), "PASS", "str1 is all alphabetic") 75 | str2 = RandomAlphabetic(21) 76 | assertTrue(t, str1 != str2, "PASS", "str1 != str2") 77 | fmt.Printf("RandomAlphabetic(21):\nstr1=%s\nstr2=%s\n", str1, str2) 78 | 79 | // random numeric 80 | str1 = RandomNumeric(21) 81 | assertEquals(t, len(str1), 21, "RandomNumeric(21) length") 82 | assertTrue(t, func() bool { 83 | numeric, _ := regexp.MatchString("^\\d+$", str1) 84 | return numeric 85 | }(), "PASS", "str1 is all numeric") 86 | str2 = RandomNumeric(21) 87 | assertTrue(t, str1 != str2, "PASS", "str1 != str2") 88 | fmt.Printf("RandomNumeric(21):\nstr1=%s\nstr2=%s\n", str1, str2) 89 | 90 | // random alpha numeric 91 | str1 = RandomAlphanumeric(21) 92 | assertEquals(t, len(str1), 21, "RandomAlphanumeric(21)") 93 | assertTrue(t, func() bool { 94 | alphanumeric, _ := regexp.MatchString("^[0-9a-zA-Z]+$", str1) 95 | return alphanumeric 96 | }(), "PASS", "str1 contains alpha or numeric") 97 | str2 = RandomAlphanumeric(21) 98 | assertTrue(t, str1 != str2, "PASS", "str1 != str2") 99 | fmt.Printf("RandomAlphanumeric(21):\nstr1=%s\nstr2=%s\n", str1, str2) 100 | 101 | // random specified chars 102 | strSet := []rune("囧ABCxyz") 103 | str1 = RandomStringSpec0(21, strSet) 104 | assertEquals(t, len([]rune(str1)), 21, "RandomSpec0(21) length") 105 | assertTrue(t, func() bool { 106 | match, _ := regexp.MatchString("^[囧ABCxyz]+$", str1) 107 | return match 108 | }(), "PASS", fmt.Sprintf("Only contains %s", "囧ABCxyz")) 109 | str2 = RandomStringSpec0(21, strSet) 110 | assertTrue(t, str1 != str2, "PASS", "str1 != str2") 111 | fmt.Printf("RandomSpec0(21):\nstr1=%s\nstr2=%s\n", str1, str2) 112 | 113 | fmt.Printf("KRand(21):\nstr1=%s\nstr2=%s\n", KRand(20, KC_RAND_KIND_ALL), KRand(20, KC_RAND_KIND_ALL)) 114 | } 115 | -------------------------------------------------------------------------------- /uuid4_test.go: -------------------------------------------------------------------------------- 1 | package gorand 2 | 3 | import ( 4 | "strings" 5 | "testing" 6 | ) 7 | 8 | // TestUUID4StringIsCorrectLength tests that the length of a generated uuid4 string is 36 characters 9 | func TestUUID4StringIsCorrectLength(t *testing.T) { 10 | l := len(NewUUID4().String()) 11 | 12 | if l != 36 { 13 | t.Errorf("String() length was incorrect, expected 36, got %d", l) 14 | } 15 | } 16 | 17 | // TestUUID4StringContainsCorrectHyphens tests that the uuid4 string contains 18 | // the correct hyphens in compliance with RFC 4122 19 | func TestUUID4StringContainsCorrectHyphens(t *testing.T) { 20 | uuidStr := NewUUID4().String() 21 | 22 | if uuidStr[8] != '-' { 23 | t.Errorf("String()[8] was incorrect, expected '-', got %d", uuidStr[8]) 24 | } 25 | 26 | if uuidStr[13] != '-' { 27 | t.Errorf("String()[13] was incorrect, expected '-' got %d", uuidStr[13]) 28 | } 29 | 30 | if uuidStr[18] != '-' { 31 | t.Errorf("String()[18] was incorrect, expected '-' got %d", uuidStr[18]) 32 | } 33 | 34 | if uuidStr[23] != '-' { 35 | t.Errorf("String()[23] was incorrect, expected '-' got %d", uuidStr[23]) 36 | } 37 | } 38 | 39 | // TestUUID4BytesReturnsCorrectBytes tests that the bytes return by Bytes are correct 40 | func TestUUID4BytesReturnsCorrectBytes(t *testing.T) { 41 | uuid := NewUUID4() 42 | bytes := uuid.Bytes() 43 | 44 | if len(bytes) != 16 { 45 | t.Errorf("Bytes() was the incorrect length, expected 16, got %d", len(bytes)) 46 | } 47 | 48 | for i, b := range bytes { 49 | if b != uuid.bytes[i] { 50 | t.Errorf("Bytes()[%d] != bytes[%d]", i, i) 51 | } 52 | } 53 | } 54 | 55 | //TestUUID4BytesNotInternalSlice tests that Bytes() does not return the same slice used internally 56 | func TestUUID4BytesNotInternalSlice(t *testing.T) { 57 | uuid := NewUUID4() 58 | bytes := uuid.Bytes() 59 | 60 | bytes[3] += 0x1 61 | 62 | if bytes[3] == uuid.bytes[3] { 63 | t.Errorf("Bytes() and bytes are referencing the same slice") 64 | } 65 | } 66 | 67 | //TestUUID4ParseStringCorrectlyParses tests that ParseString() correctly parses the string to a uuid4 68 | func TestUUID4ParseStringCorrectlyParses(t *testing.T) { 69 | str := "cc2161ae-33c1-4cb1-aa53-e81000f20a30" 70 | uuid, err := ParseString(str) 71 | 72 | if err != nil { 73 | t.Errorf("Error Parsing the string") 74 | return 75 | } 76 | 77 | if uuid.String() != str { 78 | t.Errorf("ParseString() did not correctly parse") 79 | } 80 | } 81 | 82 | //TestUUID4ParseStringCorrectlyParsesWithoutDashes tests that ParseString() correctly parses the string to a uuid4 83 | func TestUUID4ParseStringCorrectlyParsesWithoutDashes(t *testing.T) { 84 | str := "cc2161ae33c14cb1aa53e81000f20a30" 85 | uuid, err := ParseString(str) 86 | 87 | if err != nil { 88 | t.Errorf("Error Parsing the string") 89 | return 90 | } 91 | 92 | if str != strings.Replace(uuid.String(), "-", "", -1) { 93 | t.Errorf("ParseString() did not correctly parse") 94 | } 95 | } 96 | 97 | // TestUUID4ParseStringReturnsErrorOnIndex12NotValid tests that ParseString() gives an error when str[12] is not in compliance 98 | // with RFC 4122 99 | func TestUUID4ParseStringReturnsErrorOnIndex12NotValid(t *testing.T) { 100 | str := "cc2161ae-33c1-bcb1-aa53-e81000f20a30" 101 | _, err := ParseString(str) 102 | 103 | if err == nil { 104 | t.Errorf("ParseString() should have failed. str[12] is invalid.") 105 | } 106 | } 107 | 108 | // TestUUID4ParseStringReturnsErrorOnIndex16NotValid tests that ParseString() gives an error when str[16] is not in compliance 109 | // with RFC 4122 110 | func TestUUID4ParseStringReturnsErrorOnIndex16NotValid(t *testing.T) { 111 | str := "cc2161ae-33c1-4cb1-ca53-e81000f20a30" 112 | _, err := ParseString(str) 113 | 114 | if err == nil { 115 | t.Errorf("ParseString() should have failed. str[16] is invalid.") 116 | } 117 | } 118 | 119 | // TestUUID4ParseStringReturnsErrorOnBadLength tests that ParseString() gives an error when str is not 32 characters 120 | func TestUUID4ParseStringReturnsErrorOnBadLength(t *testing.T) { 121 | str := "cc2161ae-33c1-4cb1-aa53-e81000f20a" 122 | _, err := ParseString(str) 123 | 124 | if err == nil { 125 | t.Errorf("ParseString() should have failed. str is invalid length") 126 | } 127 | } 128 | 129 | // TestUUID4ParseStringReturnsErrorOnInvalidHex tests that ParseString() gives an error when str is not valid hex 130 | func TestUUID4ParseStringReturnsErrorOnInvalidHex(t *testing.T) { 131 | str := "cc2161ae-33c1-4cb1-aa53-e81g00f20a30" 132 | _, err := ParseString(str) 133 | 134 | if err == nil { 135 | t.Errorf("ParseString() should have failed. str is not hex") 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /rand.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2012-2013 Fuchun(Aying) All rights reserved. 2 | // 3 | // Licensed to the Apache Software Foundation (ASF) under one or more 4 | // contributor license agreements. See the NOTICE file distributed with 5 | // this work for additional information regarding copyright ownership. 6 | // The ASF licenses this file to You under the Apache License, Version 2.0 7 | // (the "License"); you may not use this file except in compliance with 8 | // the License. You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // 12 | // Unless required by applicable law or agreed to in writing, software 13 | // distributed under the License is distributed on an "AS IS" BASIS, 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | package gorand 18 | 19 | import ( 20 | _ "errors" 21 | _ "fmt" 22 | "math" 23 | "math/rand" 24 | _ "strconv" 25 | _ "strings" 26 | "time" 27 | ) 28 | 29 | var ( 30 | defaultRand = rand.New(rand.NewSource(time.Now().UnixNano())) 31 | ) 32 | 33 | // Creates a random string based on a variety of options, using 34 | // supplied source of randomness. 35 | // 36 | // If start and end are both 0, start and end are set 37 | // to ' ' and 'z', the ASCII printable 38 | // characters, will be used, unless letters and numbers are both 39 | // false, in which case, start and end are set to 0 and math.MaxInt32. 40 | // 41 | // If set is not nil, characters between start and end are chosen. 42 | // 43 | // This method accepts a user-supplied rand.Rand 44 | // instance to use as a source of randomness. By seeding a single 45 | // rand.Rand instance with a fixed seed and using it for each call, 46 | // the same random sequence of strings can be generated repeatedly 47 | // and predictably. 48 | func RandomSpec0(count uint, start, end int, letters, numbers bool, chars []rune, rand *rand.Rand) string { 49 | if count == 0 { 50 | return "" 51 | } 52 | if start == 0 && end == 0 { 53 | end = 'z' + 1 54 | start = ' ' 55 | if !letters && !numbers { 56 | start = 0 57 | end = math.MaxInt32 58 | } 59 | } 60 | buffer := make([]rune, count) 61 | gap := end - start 62 | for count != 0 { 63 | count-- 64 | var ch rune 65 | if len(chars) == 0 { 66 | ch = rune(rand.Intn(gap) + start) 67 | } else { 68 | ch = chars[rand.Intn(gap)+start] 69 | } 70 | if letters && ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) || 71 | numbers && (ch >= '0' && ch <= '9') || 72 | (!letters && !numbers) { 73 | if ch >= rune(56320) && ch <= rune(57343) { 74 | if count == 0 { 75 | count++ 76 | } else { 77 | buffer[count] = ch 78 | count-- 79 | buffer[count] = rune(55296 + rand.Intn(128)) 80 | } 81 | } else if ch >= rune(55296) && ch <= rune(56191) { 82 | if count == 0 { 83 | count++ 84 | } else { 85 | // high surrogate, insert low surrogate before putting it in 86 | buffer[count] = rune(56320 + rand.Intn(128)) 87 | count-- 88 | buffer[count] = ch 89 | } 90 | } else if ch >= rune(56192) && ch <= rune(56319) { 91 | // private high surrogate, no effing clue, so skip it 92 | count++ 93 | } else { 94 | buffer[count] = ch 95 | } 96 | } else { 97 | count++ 98 | } 99 | } 100 | return string(buffer) 101 | } 102 | 103 | // Creates a random string whose length is the number of characters specified. 104 | // 105 | // Characters will be chosen from the set of alpha-numeric 106 | // characters as indicated by the arguments. 107 | // 108 | // Param count - the length of random string to create 109 | // Param start - the position in set of chars to start at 110 | // Param end - the position in set of chars to end before 111 | // Param letters - if true, generated string will include 112 | // alphabetic characters 113 | // Param numbers - if true, generated string will include 114 | // numeric characters 115 | func RandomSpec1(count uint, start, end int, letters, numbers bool) string { 116 | return RandomSpec0(count, start, end, letters, numbers, nil, defaultRand) 117 | } 118 | 119 | // Creates a random string whose length is the number of characters specified. 120 | // 121 | // Characters will be chosen from the set of alpha-numeric 122 | // characters as indicated by the arguments. 123 | // 124 | // Param count - the length of random string to create 125 | // Param letters - if true, generated string will include 126 | // alphabetic characters 127 | // Param numbers - if true, generated string will include 128 | // numeric characters 129 | func RandomAlphaOrNumeric(count uint, letters, numbers bool) string { 130 | return RandomSpec1(count, 0, 0, letters, numbers) 131 | } 132 | 133 | func RandomString(count uint) string { 134 | return RandomAlphaOrNumeric(count, false, false) 135 | } 136 | 137 | func RandomStringSpec0(count uint, set []rune) string { 138 | return RandomSpec0(count, 0, len(set)-1, false, false, set, defaultRand) 139 | } 140 | 141 | func RandomStringSpec1(count uint, set string) string { 142 | return RandomStringSpec0(count, []rune(set)) 143 | } 144 | 145 | // Creates a random string whose length is the number of characters 146 | // specified. 147 | // 148 | // Characters will be chosen from the set of characters whose 149 | // ASCII value is between 32 and 126 (inclusive). 150 | func RandomAscii(count uint) string { 151 | return RandomSpec1(count, 32, 127, false, false) 152 | } 153 | 154 | // Creates a random string whose length is the number of characters specified. 155 | // Characters will be chosen from the set of alphabetic characters. 156 | func RandomAlphabetic(count uint) string { 157 | return RandomAlphaOrNumeric(count, true, false) 158 | } 159 | 160 | // Creates a random string whose length is the number of characters specified. 161 | // Characters will be chosen from the set of alpha-numeric characters. 162 | func RandomAlphanumeric(count uint) string { 163 | return RandomAlphaOrNumeric(count, true, true) 164 | } 165 | 166 | // Creates a random string whose length is the number of characters specified. 167 | // Characters will be chosen from the set of numeric characters. 168 | func RandomNumeric(count uint) string { 169 | return RandomAlphaOrNumeric(count, false, true) 170 | } 171 | 172 | type KRAND_KING int 173 | 174 | const ( 175 | KC_RAND_KIND_NUM KRAND_KING = iota // 纯数字 176 | KC_RAND_KIND_LOWER // 小写字母 177 | KC_RAND_KIND_UPPER // 大写字母 178 | KC_RAND_KIND_ALL // 数字、大小写字母 179 | ) 180 | 181 | // KRand 随机字符串. 182 | func KRand(size int, kind KRAND_KING) []byte { 183 | iKind, kinds, result := int(kind), [][]int{{10, 48}, {26, 97}, {26, 65}}, make([]byte, size) 184 | isAll := kind > 2 || kind < 0 185 | rand.Seed(time.Now().UnixNano()) 186 | for i := 0; i < size; i++ { 187 | if isAll { // random iKind 188 | iKind = rand.Intn(3) 189 | } 190 | scope, base := kinds[iKind][0], kinds[iKind][1] 191 | result[i] = uint8(base + rand.Intn(scope)) 192 | } 193 | return result 194 | } 195 | 196 | //RandInt 生成指定区间随机数. 197 | func RandInt(min, max int) int { 198 | if min >= max || min == 0 || max == 0 { 199 | return max 200 | } 201 | return rand.Intn(max-min) + min 202 | } 203 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------